PDA

Click to See Complete Forum and Search --> : simple random number?


MidgetsBro
Dec 10th, 2000, 05:45 PM
Does anyone know how to generate a random number in C++? I looked on Planet-Source-Code for some code, but the shortest was like 40 lines. I just want to get a random integer between 1 and 100. Now I realize why I use VB more that C++. The code should be no more than 20 lines of code, and even that is extreme for this simple project.

Thanks for any help.

PS I am a C++ Newbie

SteveCRM
Dec 10th, 2000, 06:14 PM
****Taken from dennis' mastermind post

only works in borland apparently

#include <stdlib.h>

int main()
{
randomize();

int peg1 = random(5) + 1;
return 0;
}

Sam Finch
Dec 10th, 2000, 07:05 PM
int seed = GetTickCount();

int rand()
{
return(((seed = seed * 214013L + 2531011L) >> 16) & 0x7fff);
}



put that at the top of your code, rand will then keep returning random integers between 0 and 524287, to get a value between 0 and 100 divide this by 5243.