rapid random number generation problem

This is a discussion on rapid random number generation problem within the C Programming forums, part of the General Programming Boards category; Hi, I need to be able to quickly generate reasonably random numbers i.e. not a series of numbers produced at ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    7

    rapid random number generation problem

    Hi,
    I need to be able to quickly generate reasonably random numbers i.e. not a series of numbers produced at one time but successive new random integers between 0 and 8 inclusive. I think my problem arises from the fact that the time period between successive generations is so short that seeding is incapable of producing randomness. I have very limited experience with random number generation. The code I have been trying follows:

    Code:
    #include "stdlib.h"
    int randnum;
    while (8 > (randnum = rand() / (RAND_MAX/8))) {
           ;
    }
    where the integer left after the above condition is used. The above code was from:
    http://www.thinkage.ca/english/gcos/.../lib/rand.html
    I'm not eternally attached to the above code so if anyone has a simple way to generate "reasonably random" integers between 0 and 8 inclusive with very little time between generations it would be highly appreciated.
    Thanks in advance, N.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I think my problem arises from the fact that the time period between successive generations is so short that seeding is incapable of producing randomness.
    Are you sure? You should only be seeding rand once; at the start of your program. If you repeatedly seed rand then the numbers you get will not be random at all.

  3. #3
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Thanks for writing in mike_g. Maybe I don't understand the process of random number generation as well as I thought. I also read that you should only seed once but if you seed rand once what is the basis for the next requested random number. In any event in previous tries seeding only once I also did not get random integers i.e. they seemed to follow a pattern which I had assumed were due either to the one seeding or the fact that the calls for numbers were so close together in time that they were starting off with the same "seed" or whatever. I've added a skeleton snippet of my code below:

    Code:
    srand(time(NULL)); /* srand returns void and is used only once to seed rand() */
    while (ok != 1) /*condition where met I keep asking for random numbers until not met*/
    {
         randnum = rand() % 8;
    }
    Thanks again for any help,
    N.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Perhaps you should do something like....

    Example:
    Code:
    randnum = (rand() >> 16) & 7;

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Thanks master5001 but I tried your idea and get the same repeated streams of integers with each iteration of the while loop (NB that with each run of the progam and therefore a new seed I get a different but nevertheless repeated series of non-random numbers). Is there a way to seed each independent time I need a number or if rand is generating a series of "random" numbers is there a way to continue along the stream without what appears to be happening i.e. starting fresh each time at the start of the series?
    I hope I'm making sense.
    Again as always thanks for the help,
    N.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Just do this:

    Example:
    Code:
    #include <time.h>
    
    ...
    
    srand(time(NULL));
    
    ...
    The reason I suggested getting the random values in that manner is so that the values use the most random bits of the random function. Its not as noticeable when you are only using eight unique values, your way has some issues with being less than truly random. But its up to you. I really do not see a huge disadvantage to throwing caution to the wind on this one.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    random() will generate the SAME sequence of numbers every time. Assuming that RAND_MAX is suffciently large and the sequence is long enought, that is not a problem as such (it's pretty difficult to remember the sequence of some rather variable set of numbers that are all 6-10 digits long when the sequence is many thousands of numbers long).

    Where in the sequence of numbers you start will be determined by the seed.

    So if you want to make a "throw dice" game, you obviously don't want your application to start with the same sequence each time. You should start at a "unpredictable point" in the sequence. Treating the current time as a seed works well as long as the time has changes suffiiciently between each time you start picking random numbers.

    And of course, if you are producing a Poker-Online game where people can win REAL money, you probably want to use something a bit better.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I doubt this guy is writing a black jack server... but for those of you who are reading this thread because you have the initiative to utilize the forum's search function, God bless you. Here is something useful:

    http://www.random.org/

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Well master5001 I tried your suggestion but no dice (so to speak). I believe matsp is correct (thanks for the background info). My program runs endlessly through the same sequence of integers but then after a great number if iterations (I'm assuming something in the time has changed sufficiently to produce a new seed point). I think I will try something to cause the program to pause between iterations? (I hate to purposely slow down a program) Is there any other way to get a seed that doesn't depend on the time?
    random.org is a very interesting site. I remember including it in a JavaScript program some time back.
    If I was writing a Blackjack program wouldn't I be looking for integers between 0 and 52 or 0 and 13 etc?
    Thanks again to all of you,
    N.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Hi again,
    I introduced a sleep for 1 second and it does look more random. Surely there has to be a better way??
    Thx, N.

  11. #11
    Registered User
    Join Date
    Sep 2008
    Posts
    13
    I don't understand why you're having a problem getting random numbers that vary.

    Just compile this code:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
    	srand(time(NULL));
    	int i;
    	
    	for(i = 0; i < 10; i++)
    	{
    		printf("Random number &#37;d: %d\n", i, rand() % 9); /* note this is 9 and not 8 because you're going from 0-8 which is 9 digits */
    	}
    	
    	getch();  /* this is added so you can view your output */
    }
    My first result was:

    Code:
    Random number 0: 5
    Random number 1: 3
    Random number 2: 7
    Random number 3: 1
    Random number 4: 2
    Random number 5: 0
    Random number 6: 7
    Random number 7: 6
    Random number 8: 2
    Random number 9: 5
    Last edited by george_1988; 09-16-2008 at 06:59 PM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,159
    I introduced a sleep for 1 second and it does look more random. Surely there has to be a better way??
    You could try using Prelude's Mersenne Twister implementation, which can be found at eternallyconfuzzled.com. Her implementation takes a hash of the return value of time(0), and the Mersenne Twister algorithm itself is pretty good.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Newton View Post
    Hi again,
    I introduced a sleep for 1 second and it does look more random. Surely there has to be a better way??
    Thx, N.
    Huh?

    Can you please post your code, since there should be no difference in the random numbers as such between having a delay or not.

    Note that you should NOT do srand() more than once in your program - it doesn't help, it just moves the point in the sequence that you get, and unless your move is completely unpredictable (that is, using a good random number generator), then you are no better off with this than you would be by simply getting the next random numer with rand().

    Calling rand many times should give you a good set of random numbers as long as you are not looking at repetitions in the range of 10000+ long sequences. That is the for the WHOLE random number, of course. Individual bits may be more or less likely to change. If you want small numbers, you may get better results if you hash the whole number, e.g.:
    Code:
    int randsmall(int max)
    {
        int r = rand();
        return ((r >> 16) ^ (r & 0xFFFF)) % max;
    }
    That will "mix" the upper 16 bits with the lower 16 bits, and make a range of 16 bits that are slightly more likely to change rapidly, particularly if the particular random number generator is generating numbers that change more in the high part than in the low part.

    And if you want something better, a Mersenne-Twister is definitely a better (but slower) random number generator.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Sep 2008
    Posts
    7
    Hi,
    The cause of my problems is that I am an idiot. Instead of calling srand once in the main program of my project I was calling it repeatedly in the function called from the main program. I have corrected this stupid error and removed the sleep() line and all is well.
    The following code (based on george_1988's) illustrates what I was foolishly doing:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
            /* srand(time(NULL)); */
            int i, j;
    
     for(j = 0; j < 4; j++)
     {
            srand(time(NULL));
            printf("Series &#37;d:\n", j);
            for(i = 0; i < 10; i++)
            {
                    printf("\tRandom number %d: %d\n", i, rand() % 9); 
            }
     }
    }
    NB that introducing a one second sleep before the srand call results in "randomness".

    I would like to thank you all for your help and interesting feedback and apologize for any frustration caused on my behalf,
    N.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    You should not be reseeding your pseudo-random number pool like that, Newton. Perhaps that is why "your way doesn't work, master5001." You should uncommment the first srand(time(NULL)) and comment out the currently used one.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random number issue
    By swgh in forum C++ Programming
    Replies: 15
    Last Post: 11-14-2008, 10:12 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 01:56 PM
  3. Problem with random number generation
    By HAssan in forum C Programming
    Replies: 1
    Last Post: 03-27-2007, 06:49 PM
  4. Random number generation
    By scrub05 in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2005, 08:20 AM
  5. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 05:25 AM

Tags for this Thread


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