|
-
Dec 10th, 2000, 06:45 PM
#1
Thread Starter
PowerPoster
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
-
Dec 10th, 2000, 07:14 PM
#2
Frenzied Member
****Taken from dennis' mastermind post
only works in borland apparently
Code:
#include <stdlib.h>
int main()
{
randomize();
int peg1 = random(5) + 1;
return 0;
}
-
Dec 10th, 2000, 08:05 PM
#3
Frenzied Member
Code:
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.
If it wasn't for this sentence I wouldn't have a signature at all.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|