Public Group

#### Archived

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

# Random numbers

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

## Recommended Posts

looking at the rand() function in "C: The complete REfrence", it says that it generates a number from 0 to 32,767. I tested it, and it does. How can I create a random number between say 1 and 10? And rand(10) does not work.

##### Share on other sites
Simple. Just used the mod (%) operator and add one.

  int numberBetween1And10 = (rand() % 10) + 1;

Note that rand()%10 returns a number between 0 and 9, so you need to add 1 to make it between 1 and 10. Hope that helps!

##### Share on other sites
yes, that did the trick. Thanks so much.

edit: Well, one problem, though. Just to try it out, I put in the following :

  int i;	clrscr();	for(i=0; i<50; i++)	{		int num = ((rand() % 5) + 1) +((rand() % 5) + 1) +((rand() % 5) + 1) + 3;		printf("%d \n", num);	}	getch();

This is supposed to give a number between 3 and 18. (Dungeons and dragons ability scores.) It does give numbers between 3 and 18, but it gives the same numbers each time I run it. Is there a way to make it come up with unique numbers each time the program is ran?

[edited by - mrtie_dye on May 26, 2003 3:45:17 PM]

##### Share on other sites
quote:
Original post by mrtie_dye
yes, that did the trick. Thanks so much.

edit: Well, one problem, though. Just to try it out, I put in the following :

    int i;	clrscr();	for(i=0; i<50; i++)	{		int num = ((rand() % 5) + 1) +((rand() % 5) + 1) +((rand() % 5) + 1) + 3;		printf("%d \n", num);	}	getch();

This is supposed to give a number between 3 and 18. (Dungeons and dragons ability scores.) It does give numbers between 3 and 18, but it gives the same numbers each time I run it. Is there a way to make it come up with unique numbers each time the program is ran?

[edited by - mrtie_dye on May 26, 2003 3:45:17 PM]

I think the function you are looking for is SRAND. It seeds the random number generator.

just pass it the time or something and you''ll be good.

##### Share on other sites
As already said, use srand(). Here is how you use it:
#include <cstdlib>   // for rand() (and srand())#include <ctime>     // for time()int main(){    ...    srand( unsigned (time(0)) );    ...    return 0;}

Incidently, if you want a number between 3 and 18:

int num = rand() % 16 + 3

[ Google || Start Here || ACCU || MSDN || STL || GameCoding || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on May 26, 2003 4:00:56 PM]

##### Share on other sites
quote:

Incidently, if you want a number between 3 and 18:

int num = rand() % 16 + 3

DOH! I was thinking roll 3 six sided dice and add them together. Your way is much better. Man, I gotta get my brain out of the frying pan for a little while.

##### Share on other sites
quote:
Original post by mrtie_dye
DOH! I was thinking roll 3 six sided dice and add them together. Your way is much better. Man, I gotta get my brain out of the frying pan for a little while.

Actually, generating a random number between 3 and 18 is *not* the same as generating three random numbers between 1 and 6 (ie. 3 dice). Rolling muliple dice create a bell curve, with each of the middle values (for three dice, this would be 9, 10, 11, 12) being more likely than values on the edges (3, 4, 5, 16, 17, 18).

If you think about it, you'll see why: for example, there is only one way to get a sum of 3 (each dice as a value of 1). However, there are *27* ways to get a sum of 10 (6+3+1, 6+2+2, 6+1+3, 5+4+1, 5+3+2, 5+2+3, 5+1+4, etc).

If you want to simulate 3 dice, use the method you had before with the 3 calls to rand. If you want a straight linear random between 3 and 18, use the one call method.

[edited by - BriTeg on May 26, 2003 4:25:50 PM]

##### Share on other sites
I'd just like to point out, that

round((rand() / MAX_RAND) * 4) + 1

Will give you better random numbers than

rand() % 5 + 1

(I realized that both functions returned differnt values)

[edited by - dede on May 28, 2003 12:57:36 PM]

Define better.

##### Share on other sites
quote:
Original post by BitBlt
Simple. Just used the mod (%) operator and add one.

    int numberBetween1And10 = (rand() % 10) + 1;

Note that rand()%10 returns a number between 0 and 9, so you need to add 1 to make it between 1 and 10. Hope that helps!

Why cant you do something like (rand() % 11) ???

BattleGuard

Whenever I try to find a better signature than this... Well, I can''t... This is it, Sorry...

1. 1
Rutin
23
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 33
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631745
• Total Posts
3002001
×