Results 1 to 3 of 3

Thread: simple random number?

  1. #1

    Thread Starter
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    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
    <removed by admin>

  2. #2
    Frenzied Member
    Join Date
    Jul 1999
    Posts
    1,800
    ****Taken from dennis' mastermind post

    only works in borland apparently
    Code:
    #include <stdlib.h>
    
    int main()
    {
    	randomize();
    
    	int peg1 = random(5) + 1;
       return 0;
    }

  3. #3
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    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
  •  



Click Here to Expand Forum to Full Width