![]() |
| | #1 |
| Registered User Join Date: Sep 2008
Posts: 7
| rapid random number generation problem 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))) {
;
}
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. |
| Newton is offline | |
| | #2 | |
| Wheres the lesbians? Join Date: Oct 2006 Location: UK
Posts: 1,219
| Quote:
__________________ Senior highbrow doctor of authority. | |
| mike_g is offline | |
| | #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;
}
N. |
| Newton is offline | |
| | #4 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Perhaps you should do something like.... Example: Code: randnum = (rand() >> 16) & 7; |
| master5001 is offline | |
| | #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. |
| Newton is offline | |
| | #6 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| Just do this: Example: Code: #include <time.h> ... srand(time(NULL)); ... |
| master5001 is offline | |
| | #7 |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| 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. |
| matsp is offline | |
| | #8 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| 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/ |
| master5001 is offline | |
| | #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. |
| Newton is offline | |
| | #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. |
| Newton is offline | |
| | #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 %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 */
}
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 05:59 PM. |
| george_1988 is offline | |
| | #12 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,368
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is offline | |
| | #13 | |
| Kernel hacker Join Date: Jul 2007 Location: Farncombe, Surrey, England
Posts: 15,686
| Quote:
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;
}
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. | |
| matsp is offline | |
| | #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 %d:\n", j);
for(i = 0; i < 10; i++)
{
printf("\tRandom number %d: %d\n", i, rand() % 9);
}
}
}
I would like to thank you all for your help and interesting feedback and apologize for any frustration caused on my behalf, N. |
| Newton is offline | |
| | #15 |
| Banned Join Date: Aug 2001 Location: Visalia, CA, USA
Posts: 3,699
| 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. |
| master5001 is offline | |
![]() |
| Tags |
| generation, number, random, rapid |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Random number issue | swgh | C++ Programming | 15 | 11-14-2008 10:12 AM |
| adding a number to a number | bigmac(rexdale) | C Programming | 11 | 10-24-2007 12:56 PM |
| Problem with random number generation | HAssan | C Programming | 1 | 03-27-2007 05:49 PM |
| Random number generation | scrub05 | C++ Programming | 5 | 01-07-2005 08:20 AM |
| Prime number program problem | Guti14 | C Programming | 11 | 08-06-2004 04:25 AM |