Random-numbers?

B_old
09-10-2002, 06:06 AM
Hello.

Can anyone tell me what the easiest way is to get random numbers between to values? That would be great.
Thank you!

Iceplug
09-10-2002, 07:03 AM
If I remember correctly, you use the % operator to scale the numbers down and then add to raise them.
So...

Number = Low + (Rand() % (High - Low));

Hope this works...

B_old
09-10-2002, 09:22 AM
Well, It says that Rand() is a undeclared identifier...
Maybe I have to include som *.h?
Well thanks anyway.

reboot
09-10-2002, 09:29 AM
Try rand()

(c is case sensitive)

#include <stdlib.h>

Thinker
09-10-2002, 09:29 AM
<stdlib.h>

B_old
09-10-2002, 09:39 AM
Oh yes. it works. Thanks

Also I wonder if it is only possible to have 8 parameters in a function, can this be true?

B_old
09-10-2002, 09:51 AM
Hmmmm, I get random numbers indeed.
But the Low-value must be 0 it seems.
Else I cannot control the High-value somehow...

saryon
09-16-2002, 05:32 PM
the low value and the high value are ones you have to create...so basically what he's saying

if you want to create two numbers between 4 and 10


Number = Low + (Rand() % (10 -4));

B_old
09-17-2002, 02:06 AM
Ah, OK, very well then.
I could fortunately use it already though, despite my complaining.
Thanks

noRulez
09-21-2002, 07:52 AM
for future readers, it'd be a good idea to seed your random # generator with the current time. This requires including time.h, as well as a simple statement:
srand ( time(NULL) );

Unfortunately, it's impossible to get a truly random # from a computer. Silly things.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum