# how do i make a code that choosea a random number from 1 to 100?

Show 80 post(s) from this thread on one page
Page 2 of 2 First 12
• 10-26-2001
zen
Quote:

rand() % 99 + 1 will give you 100. If the random number is 99+1, do the math in your head.
No the modulus operator produces a remainder, there is no value that you could divide by 99 and add 1 that would produce 100.

98%99=98+1=99
99%99=0+1=1

197%99=98+1=99
198%99=0+1=1

etc...

but

99%100=99+1=100 //What we want
100%100=0+1=1
• 10-26-2001
Magos
Zen is right
Zen is right.

To get the output 100 from rand()%99+1, rand()%99 must be 99. What rand()%99 do is divide the number generated from rand() with 99, and return the remainder from the division.

Example:

5%5=0 (5/5=1, Remainder=0, 1*5=5)
10%5=0 (10/5=2, Remainder=0, 2*5=10)
11%5=1 (11/5=2, Remainder=1, 2*5+1=11)
13%5=3 (13/5=2, Remainder=3, 2*5+3=13)
29%5=4 (29/5=5, Remainder=4, 5*5+4=29)

30%5=5 (Not correct, you can divide (subtract) the remainder once more)

As you can see, the remainder cannot be equal or higher than what you divide with, which makes it impossible for rand()%99 to generate the number 99.
• 10-26-2001
Forever
Wow i wanted to know the same question

but how do u change the random numbers so they are smaller than 100 and bigger than 1

thanks for any answer
• 10-26-2001
Unregistered
random number between 35 and 40 inclusive:

srand() or randomize() once as per previous discussion:

x = 35 + (rand() % 6)
• 10-26-2001
Magos
I'm not sure what you mean, but I'll make a try:

If you want a random number from 10 to 19, you use this: rand()%10+10.

General syntax: rand()%Range+Start
Range is how many different numbers you want (from Start to (Start+Range-1) ).
Start is the first number (in the above case: 10)

Example:
rand()%5+10 A random number from 10 to 14
rand()%20+80 A random number from 80 to 99
rand()%100+100 A random number from 100 to 199

Perhaps you want a random number from all even numbers between 10 and 19 (10, 12, 14, 16 & 18):
2*(rand()%5)+10

-Hope you didn't get even more confused :)
• 10-26-2001
Magos
PS
PS: I Love to use different colours in the text, as you may have noticed ;).
• 10-26-2001
Forever
Thanks Magos =)
• 10-26-2001
bobish
You would use rand()%100 + 1
rand()%100 = 0-99 because it give you the remainder there is no way to get a reamainder of 100 when dividing by 100.
• 10-27-2001
ii3ejoe
thanks i got the rand part working now.....
to make it into an actually game-like program......
do the prompts to user..........cin the data.......then use a for loop to count from 10 to 0 (number of try the user gets to guess the number correctly)......

example
guess // is number entered by user
rand_num// is the rand number the program generates
answer// either 'Y' or 'N' meaning yes or no to play game again.

for(int i=0;i<=10;i--)
{
if (guess!=rand_num)
{

cout << "YOu have "<<i<<"tries left.\n";
}
else if (guess==rand_num)
{
cout<< "Good Work............You got the number in ony " << i << " tries.\n";
}
hows that look?

i was thinking of adding....... this at the end
}while(answwer=='y'||'Y')
}
• 10-28-2001
hehe
Use srand() and rand()%101.

what is so great about rand()%100+1, :)
• 10-28-2001
zen
The original post asked about a random number generator between 1-100, and rand()%100+1 will do this.

rand()%101 will produce random numbers between 0-100.
Show 80 post(s) from this thread on one page
Page 2 of 2 First 12