Generating Random Numbers in Visual C++

pac1085

Diamond Member
Jun 27, 2000
3,456
0
76
Anyone know how to do this? In borland, I just use stdlib.h, but that doesnt work in VC++6. Heres an except of what I did:

void GetInts(int highNum, int & x, int & y)
{
randomize();
x=(random(highNum)+1);
y=(random(highNum)+1);
}

Works fine in Borland, but not in MSVC++.

 

pac1085

Diamond Member
Jun 27, 2000
3,456
0
76
OK, I just tried rand(); and that error went away...im still getting the other error, "C:\TempC++\FlashCards.cpp(85) : error C2065: 'random' : undeclared identifier" for
x=(random(highNum)+1);
y=(random(highNum)+1);
 

ViRGE

Elite Member, Moderator Emeritus
Oct 9, 1999
31,516
167
106
If you want, I have a whole class for generating random numbers for both Borland and VC++. Just give me the word, and I'll email it to ya, along with the instructions.
 

bUnMaNGo

Senior member
Feb 9, 2000
964
0
0
srand(seed); // where seed is a number to start from I guess... I think you need to #include <stdlib.h>
 

FatAlbo

Golden Member
May 11, 2000
1,423
0
0
Include time.h and stdlib.h

srand(time(NULL));

This uses the number of seconds after midnight of the current day (return value of time(NULL)) as the seed value. Don't call srand() more than one time in your program or else the numbers won't be random due to the seed value being changed. Use rand() to generate the random number.