Archived

This topic is now archived and is closed to further replies.

Ragadast

Randomize

Recommended Posts

Lohrno    222
Hehe this might be a little unclear, so I''ll explain:

Say you wanted to have a random number from 1 to 10.

First, you should seed the random number generator with something, preferably a time function, like timeGetTime(), or clock(), so it''s always different. Do this only once in the very beginning of your program.

Then, to get a random number, you would use the random function something like this:

rand()%10 would give you a random number from 1 to 10. (Or 0 to 9, I forget)

rand()%20 would give you ..... 1 to 20

if you want from other numbers than 0 or 1, you have to use math.

like if you want a number from 10 to 20, you should do something like

rand()%10 + 10;

get it? =D

-=Lohrno

Share this post


Link to post
Share on other sites
IndirectX    122
Never use modulus with rand. Use division instead, or you''ll screw up even distribution:

int random_number = float(rand())/RAND_MAX*max;
int random_number = lower_bound+float(rand())/RAND_MAX*(upper_bound-lower_bound);

Share this post


Link to post
Share on other sites
bangz    119

  
#include <stdlib.h>
#include <iostream.h>
#include <time.h>

void main()
{
srand( (unsigned)time( NULL ) ); // generate your random seed



cout << "Random number between 0 and 9: " << rand()%10 << endl;
cout << "Random number between 0 and 19: " << rand()%20 << endl;
cout << "Random number between 0 and 99: " << rand()%100 << endl;
cout << "Random number between 0 and 5000: " << rand()%5001 << endl;
}


EDIT: forget source brackets ;-)

[edited by - bangz on May 5, 2002 9:23:09 PM]

Share this post


Link to post
Share on other sites
Carrot    136
quote:
Original post by IndirectX
Never use modulus with rand. Use division instead, or you''ll screw up even distribution:



How would this affect the distribution?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
If I wanted to get a random number in plain ansi c,
would I also have to create a ´seed´ first or use the time function? if do, how would I go about doing this?
thanx in advance

PEACE

Share this post


Link to post
Share on other sites
Kern    124
Carrot : if RAND_MAX == 10 and you call rand() % 3 the probability would be : 0=4 1=3 2=3. In the long run this makes for a big mistake and since that mistake is alway in the low number it imbalance things.

You should use this formula to have balanced probability :
Result = int(rand() / (RAND_MAX + 1.0f) * Delta)

Share this post


Link to post
Share on other sites
Carrot    136
quote:
Original post by Kern
Carrot : if RAND_MAX == 10 and you call rand() % 3 the probability would be : 0=4 1=3 2=3. In the long run this makes for a big mistake and since that mistake is alway in the low number it imbalance things.



Good point,thanks.

Share this post


Link to post
Share on other sites
civguy    308
There honestly should be a FAQ for this random stuff. It's asked every month here and every month there are ppl saying "use rand()%x" and other ppl correcting them.
* S I G H *

Although in a game, I don't think a slightly biased distribution is noticeable.

[edited by - civguy on July 22, 2002 10:25:08 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Before you can use rand() you need to seed the random number generator with srand(TIME(NULL)) or similar.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
32767?!?!?

What do you mean windows is limited? hehehehe

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
in my text book, this is how they say you get a random int:
// place this at top of int_main
void rand_seed()
{ int seed = static_cast<int>(time(0));
srand(seed);
}

// gives you random int in the range you specify
int rand_int(int bottom_range, int top_range)
{ return bottom_range + rand() % (top_range - bottom_range + 1);
}

Share this post


Link to post
Share on other sites