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 1 of 2 12 Last
• 10-25-2001
ii3ejoe
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 :)
• 10-25-2001
gliptic
(rand()%100)+1
• 10-25-2001
Magos
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...
• 10-25-2001
electricglidingman
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";

}
• 10-25-2001
Dalren
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 :)
• 10-25-2001
Aran

use code tags.

thank you.
• 10-25-2001
Unregistered
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;
}
• 10-25-2001
ii3ejoe
ummmmmmmmm ok whos right :)
i tried a few all gave at least 2-6 errors
• 10-25-2001
itld
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-25-2001
gliptic
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.
• 10-25-2001
Thantos
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.
• 10-26-2001
Magos
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 :).

Quote:

P.S. Nice avatar Magos, but i belive the character from CT was Magus not Magos
• 10-26-2001
electricg

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

Just check if you're trying to compile these codes in QBasic.
• 10-26-2001
Stoned_Coder