Skip to main content
GameDev.net gamedev.net
🔒 Locked

Random number generation

Started by Auxil Nov 7, 2000 at 7:22 PM 19 replies 1.1k views
Original Post
Auxil
Auxil
I was wondering if anyone knew how to have c++ generate a random number within a given range. Like 0 and 65, and have the program choose any number inbetween. I asked my teacher, but all she told me was how the computer does it, or something like that, using the monte carlo method or something, it wasn''t helpful. Anyways, i would really appreciate any feedback on this thanks!
Nazrix
Nazrix
You just use

    
rand()%65;



"All you touch and all you see is all your life will ever be --Pink Floyd
Quidquid latine dictum sit, altum viditur.
Need help? Well, go FAQ yourself.
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Pouya
Pouya
*shock*
Nazrix.... you... you.... you helped him!!!???

kiddin

...
Nazrix
Nazrix
Yeah, I know....suprised me too

I didn't give him incorrect information, or didn't try to convince him I'm cool or anything


"All you touch and all you see is all your life will ever be --Pink Floyd
Quidquid latine dictum sit, altum viditur.
Need help? Well, go FAQ yourself.


Edited by - Nazrix on November 7, 2000 8:46:30 PM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Auxil
Auxil
is there a certain header file needed for that? Also, how would i store that to a variable?

Thanks, btw, for the help
ByteBlaster256
ByteBlaster256
If the random number generation is time-critical, and you need to be able to optimize the code, then try something like this:

long GenerateRandomNumber(long Minimum, long Maximum, long* pS)
{
Seed = *pS;
Seed = Seed * PrimeNumber1;
if(seed > BigHugeUpperLimit)
Seed = Seed / PrimeNumber2;
*pS = Seed;
return (Seed%(Maximum-Minimum))+Minimum;
}

This function should work if PrimeNumber1 is smaller than PrimeNumber2. BigHugeUpperLimit should be equal to the biggest value "Seed" can hold divided by PrimeNumber1, so you''ll never have too big a number in Seed. In stead of the pointer stuff, you can also use a static variable. Note: these numbers will seem fairly random, but don''t use them to create large patterns that can be seen all at once, because in truth these random numbers aren''t all that random, and patterns will begin to emerge. Also: use large prime numbers for more randomness. I usually use 5-digit primes.

-----------------------------
C++ is great, but when is B-
coming out?
-----------------------------C++ is great, but when is B-coming out?
Nazrix
Nazrix
I forget what header file it uses now...

but if you want to store the number in a variable you just do this:

    
int number;
number=rand()%65;





"All you touch and all you see is all your life will ever be --Pink Floyd
Quidquid latine dictum sit, altum viditur.
Need help? Well, go FAQ yourself.
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Auxil
Auxil
great, i will ask my teacher about the header, or if anyone knows, save me some time, tomarrow is alst day for the school week, and i probably won''t get an answer from her tomarrow

Anyways, thanks to you both for your help, i really appreciate it
Pouya
Pouya
it''s either math.h or stdlib.h
just use both to be safe

...
Auxil
Auxil
ok, the header is the stdlib.h

when i do this, i always get the same number, which, in this case, is 41. is there anyway to have it different everytime, or am i going to have to use the number=rand()%65 everytime i want a new number?
Nazrix
Nazrix
I think you can do this in order to get different numbers each time...
You'd only have to call this once at the beginning though.

srand(GetTickCount());


"All you touch and all you see is all your life will ever be --Pink Floyd
Quidquid latine dictum sit, altum viditur.
Need help? Well, go FAQ yourself.


Edited by - Nazrix on November 7, 2000 9:31:35 PM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Pouya
Pouya
just use randomize();

...
Nazrix
Nazrix
there''s a randomize(); function in C++? I didn''t know that.


"All you touch and all you see is all your life will ever be --Pink Floyd
Quidquid latine dictum sit, altum viditur.
Need help? Well, go FAQ yourself.
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Auxil
Auxil
can''t get srand(GetTickCount()) to work, but using the whole number=rand... statement over again works. i''ll fiddle with the srand thing, it might come to me

Thanks again for all your help
Auxil
Auxil
What is the header for randomize?
and how would it be used?
(did i mention at anytime that i know next to nothing?)
Arjan
Arjan
To my knowledge, randomize() is only available in Borland C, NOT in VC!

Just stick to the following:

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

int main()
{
int number;

//this will initialise the randomizer

srand( (unsigned)time(NULL) );

//use rand()%x to get a random value in the range 0...x-1
number = rand()%1000;

cout << "A random number: " << number << endl;

return 0;
}


Hmm, it doesn't look like I want it, even though I use [ source ] and [ /source ]..



Edited by - Arjan on November 8, 2000 3:33:44 PM

Edited by - Arjan on November 8, 2000 3:44:51 PM
--------------------------Programmers don't byte, they nibble a bit. Unknown Person-------------------------
furby100
furby100
And you only call srand() once at the beginning of your program, you don''t call it every time you want to generate a random number. For example:
    
#include <stdio.h>

#include <stdlib.h>

#include <time.h>

void main()
{
int number1, number2;
srand((unsigned)time(NULL));
number1 = rand()%30;
number2 = rand()%30;
printf("I have thought of two numbers between 0 and 30.\nThey are: %d, %d", number1, number2);
}





Please state the nature of the debugging emergency.


sharewaregames.20m.com

furby100
furby100
If you want that in C++ version, here it is:
    
#include <iostream.h>

#include <stdlib.h>

#include <time.h>

void main()
{
int number1, number2;
srand(unsigned(time(NULL)));
number1 = rand()%30;
number2 = rand()%30;
cout << "I have thought of two numbers between 0 and 30," << endl << "They are: "
<< number1 << number2;
}



Please state the nature of the debugging emergency.


sharewaregames.20m.com

Edited by - furby100 on November 8, 2000 4:51:29 PM

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.