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

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

i was messing around an thought that id try to make a simple DOS "Number Guessing " game, i almost all the code thought up..cept the part to pick the rand0m number?

anyone know how this can be done?........ if so thanks in advance

2. (rand()%100)+1

randomize();

int Var=random(100)+1; //Variable is set to a number from 1 to 100

random(n); returns an integer number from 0 to n-1.
You must include Stdio.h, Stdlib.h or something. can't really remember...

4. Here's maybe an easier way

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

int main () {
int randomnumber;
srand (time(0));
randomnumber = 1+rand() %99;

cout<<randomnumber<<"That's incredible!/n";

}

5. Well since no one seems to have it completly right yet, I guess ill give it a try

first you will need to include stdlib.h or cstdlib and time.h

now the first thing your program needs to do is randomize the random number generator so you get new numbers everytime
so you use the line of code "srand(time(0));" the key is make sure that that line is only run once or your program will crash

next to get a random number you use rand();
rand() returns a number between 0 and some large number i think its about 32000, but thats not important.

so if you use rand()%100 you will get a number between 0 and 99, but you want a number between 1 and 100 so you would want to use rand()%100+1 and that will return a number between 1 and 100

P.S. Nice avatar Magos, but i belive the character from CT was Magus not Magos

use code tags.

thank you.

7. Since you are all mistaken, try this.

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

int main()
{
int random_number = 0;

srand(time(NULL));

random_number = rand() % 99 + 1;

cout << random_number << endl;

return 0;
}

8. ## ummmmmmmmm ok whos right :)

i tried a few all gave at least 2-6 errors

9. ## uuuuuuum

i tried unregistered guests version and my borland 5 compiler compiled and linked with one simple error - i changed <<endl; to <<"\n";
so at least he/she got it right.

NOW to my question.
what purpose does (time) serve in a random number generator?

m.r.

10. This is wrong: rand()%99 + 1
You will never get 100 with this code

This is right: rand()%100 + 1

The time is used as the seed, the original number in the random generator. The number is then scrambled in different ways to get new random numbers.

11. The purpose of (time) is to set the seed value of rand() to the current time. This is because the time will always be different so the same number won't come up in the same order every time.

12. ## To Dalren

To Dalren:

Thanks! But I am not Magus, I am Magos (MAGnus OStberg). Though we both look alike, we are not the same persons .

P.S. Nice avatar Magos, but i belive the character from CT was Magus not Magos

13. ## ..

2-6 errors for each one? Geez what compiler are u using?

Just check if you're trying to compile these codes in QBasic.

#include<cstdlib>

The rand() and srand() funcs. are in that library.

15. rand() % 99 + 1 will give you 100. If the random number is 99+1, do the math in your head.

%100 + 1 will be 101.