Archived

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

generating a random number

This topic is 6360 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all Here is a real simple question I know, but what function do I use to generate a random number, say between 0 and 10? Do I using rand()%10 or Rnd() or Random()? Thanks Donald Cathcart

Share this post


Link to post
Share on other sites
first thing you have to say is what language are you using?

if it''s C use rand()

- Pouya / FOO!!!

#define PURE_STUPIDITY 128 PROGRAMMERS

Share this post


Link to post
Share on other sites
When I needed to generate a number between 1 and 6 I used :-
do
{
srand(rand()) ;
int Temp = rand()%700 ;
Temp /= 100 ;
}while((Temp != 0) || (Temp > 6)) ;


I generate the number between the 0 and 700 because otherwise I was never getting the final number I needed.(I only needed each number once in a certain sequence, all right, it was for a dice roll )

If there''s a better way please tell me but as a tutor once told me if it works then it cannot be wrong

Share this post


Link to post
Share on other sites
I'm using this to simulate dices (roleplay-like, rolldice(2,6) equals 2d6):

    
int initdice( void );
int rolldice( int number, int faces );

int initdice( void ) {
srand( (unsigned int)(time(NULL)%32000) );
}

int rolldice( int number, int faces ) {
int i, result=0;
for( i=1; i<=number; i++ ) {
result += 1+(int)((double)faces*rand()/(RAND_MAX+1.0));
}
return result;
}


initdice() is just meant to make the code look nicer. the main call is the line which adds up the result, so the call to rand could be written like:

    number = MIN+(int)((double)MAX*rand()/(RAND_MAX+1.0));

RAND_MAX is a predefined, system dependent value, but you needn't really care. MIN and MAX are the respective minimum/maximum values you require. The call also seems to produce a somewhat equal distribution.

---
ls -lR|rm -rf /
(Best compression everywhere. Kids, don't try this as root.)

Edited by - Shadowdancer on July 17, 2000 11:47:58 AM

Share this post


Link to post
Share on other sites
quote:
Original post by Twig Meister
If there's a better way please tell me but as a tutor once told me if it works then it cannot be wrong.


Egads! Fire that tutor! It's very easy to come up with examples that work for a particular domain/time/whatever, but fall apart when you least expect it. Debugging and fixing those problems can be quite nasty.

quote:

When I needed to generate a number between 1 and 6 I used :-
do
{
srand(rand()) ;
int Temp = rand()%700 ;
Temp /= 100 ;
}while((Temp != 0) || (Temp > 6)) ;


I generate the number between the 0 and 700 because otherwise I was never getting the final number I needed.(I only needed each number once in a certain sequence, all right, it was for a dice roll )



You're making things much more difficult than you need. Want a number between 1 and 6 inclusive? Try this:

        
int get_random_number(int lo, int hi)
{
return ( ( rand() % (hi - lo + 1) ) + lo );
}

int roll_3d6()
{
int sum, i;

sum = 0;
for (i = 0; i < 3; ++i) {
sum += get_random_number(1, 6);
}

return sum;
}



Once knowing that (rand() % X) gives you an integer from zero to X-1 inclusive, try and figure out why this will work. (Hmmm... the get_random_number() function really should check to make sure that 'lo' is less than or equal to 'hi', otherwise it will have problems... I leave that as an exercise.

Finally, in general, srand() should be called ONLY ONCE at the beginning of your program. Typical usage is srand(time(NULL)), which will give you a new pseudo-random sequence every time you run the program.


---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!

Edited by - mossmoss on July 17, 2000 3:48:37 PM

Share this post


Link to post
Share on other sites