Generating Random Numbers

This is a discussion on Generating Random Numbers within the C++ Programming forums, part of the General Programming Boards category; I am making this small program wich generates random numbers in order to make random sums. I checked the toturial ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    50

    Generating Random Numbers

    I am making this small program wich generates random numbers in order to make random sums. I checked the toturial to see how to generate random numbers using the system time, wich is never the same ofcourse. The problem is that I need to generare multiple random numbers at exactly the same time.


    This is a piece of code I used. It will generate differend numbers IF the user does not press enter 3 times a second..



    cout<<"Strike any key to generate first value";
    cin.get();
    time_t x;
    time(&x);
    srand((unsigned int)x);
    sumvalueA = rand() % (lowX-highY/lowX)+lowX;


    cout<<"Strike any key to generate second value";
    cin.get();
    int sumvalueB=0;
    time_t y;
    time(&y);
    srand((unsigned int)y);
    sumvalueB = rand() % (lowX-highY/lowX)+lowX;

    cout<<"Strike any key to generate random sum";
    cin.get();
    int randomsum=0;
    time_t z;
    time(&z);
    srand((unsigned int)z);
    randomsum= rand() % (randsumX-randsumY/randsumX)+randsumX;


    The outcome was 3 different values when the user waited a while before pressing a key, but faster pressing a key would make the same values.

    Is there a different way of creating random numbers, without having to use the system time??
    I dont know half as many of you half as well as I should like, and I like less than half of you half as well as you deserve...

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    You should only call the srand() function once at the start, not every time you want to call rand(). rand() will then produce a pseudo-random number each time. By restarting the generator with the same seed value, you will start the same pseudo-random sequence each time - as you have seen.

    Code:
    time_t x;
    time(&x);
    srand((unsigned int)x);
    
    cout<<"Strike any key to generate first value";
    cin.get();
    sumvalueA = rand() % (lowX-highY/lowX)+lowX;
    
    cout<<"Strike any key to generate second value";
    cin.get();
    int sumvalueB=0;
    sumvalueB = rand() % (lowX-highY/lowX)+lowX;
    
    cout<<"Strike any key to generate random sum";
    cin.get();
    int randomsum=0;
    randomsum= rand() % (randsumX-randsumY/randsumX) +randsumX;
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    50
    Isn't there any oter way of generating random numbers without having to use the system time?? I still have this frequecy in the random numbers.. Like this:

    24
    29
    34
    44
    57

    It's just NOT 100% random.. I can still see it uses the system clock..
    I dont know half as many of you half as well as I should like, and I like less than half of you half as well as you deserve...

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Did you read this and this ?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    >>> It's just NOT 100% random

    The second link from Hammer explains how the pseudo-random number sequence is generated and indicates some of the pitfalls.

    Without special equipment, you will never get truly random numbers. Typical true random number generators of the likes used by electronic lottery systems use a radioactive isotope, older systems use noise diodes, but these tend to wear out after a period of use.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    50
    Yes I KNOW! adrianxw told me before! And, you're wrong about the link thing, cuz there weren't any other links posted but yours when I posted my previous reply. And I cant use a link to some "use code tags" post anyway(yes I know I should use code tags, but I don't have alot of time) BTW, I didn't ignore any link, how could I ignore non-existing links?? Don't post nonsense plz.

    But thx for the extra info anyway
    I dont know half as many of you half as well as I should like, and I like less than half of you half as well as you deserve...

  7. #7
    UseCodeTags
    Guest
    "I know I should use code tags, but I don't have alot of time"

    There are those who don't bother answering when you don't - you are reducing the number of potential helpers by not.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 09:47 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 06:20 AM
  3. Criteria based random numbers
    By alkisclio in forum C++ Programming
    Replies: 6
    Last Post: 09-14-2006, 01:29 PM
  4. Random Number Generating
    By K.n.i.g.h.t in forum C Programming
    Replies: 9
    Last Post: 01-30-2005, 02:16 PM
  5. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21