#### Archived

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

# Random numbers

This topic is 5315 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...

##### Share on other sites
Besides the srand( time(NULL) ) statement that you need to randomize the rand() function (that''s how I think of it although I''m sure it''s not the correct terminology), the statement

int num = ((rand() % 5) + 1) +((rand() % 5) + 1) +((rand() % 5) + 1) + 3;

is incorrect. To simulate 3 die being rolled change it a tiny bit:

int num = ((rand() % 6) + 1) +((rand() % 6) + 1) +((rand() % 6) + 1);

and you don''t want to do:

int num = (rand() % 16) + 3;

for the reasons previously stated. It will not simulate 3 die, although it will give a number between 3-18.

##### Share on other sites
quote:
Original post by Edward Ropple
Define better.

You'll get a better/more even distribution of random numbers with MAX_RAND.

Lets say my random number generater generates number generator generates numbers between 0-7..., and I want a random number between 0 - 5(d6), so

0 mod 6 = 0
1 mod 6 = 1
2 mod 6 = 2
3 mod 6 = 3
4 mod 6 = 4
5 mod 6 = 5
6 mod 6 = 0
7 mod 6 = 1

I have a bias towards 0 and 1.

0/7 * 5 = 0 (0)
1/7 * 5 = 0.71(1)
2/7 * 5 = 1.42(1)
3/7 * 5 = 2.14(2)
4/7 * 5 = 2.86(3)
5/7 * 5 = 3.58(4)
6/7 * 5 = 4.29(4)
7/7 * 5 = 5.0 (5)

Here I have a bias towards 1 or 4.

Having a bias towards 1 or 4 is slightly better than a bias of 0 or 1. Mod usually will be bias towards the lower values, while MAX_VALUE will usually be more bias towards the center.

So lets say our hero has to get a 4 or 5, or he dies...(typical D&D game) so there is 1/3 chance in total random die.(0-5 = 6)

2/6 = 0.33333333

for mod -> 2/8 = 0.25
for MAX_VALE 3/8 = 0.375

MAX_VALUE better aproximates the correct dice roll, giving a slight bias towards winning, which is also a good thing(tm) in RPGS

(edit - Confusing, fixed it up)

[edited by - dede on May 27, 2003 7:57:19 PM]

##### Share on other sites
quote:

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

rand()%11 will give you a rand num between 0 and 10. not 1 and 10.

doh, nuts. Mmmm... donuts
My website

• ### Forum Statistics

• Total Topics
628710
• Total Posts
2984331

• 23
• 11
• 9
• 13
• 14