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
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
(rand()%100)+1
// Gliptic
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...
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
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";
}
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
Proud to be a gun carrying, freedom loving, libertarian
Don't listen to Right-Wing propaganda, legalize it NOW!
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;
}
i tried a few all gave at least 2-6 errors
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.
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.
// Gliptic
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.
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
MagosX.com
Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime.
2-6 errors for each one? Geez what compiler are u using?
Just check if you're trying to compile these codes in QBasic.
try adding
#include<cstdlib>
The rand() and srand() funcs. are in that library.
Free the weed!! Class B to class C is not good enough!!
And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi
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.