473,395 Members | 1,457 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

random number not fast enough

175 100+
I've got an issue with the random number generator I'm using in C++. It's randomness is fine, but the problem is that I'm automating something that runs a few thousand times in a couple of seconds, and it's going so fast that the random numbers don't change.

If I'm manually running the code and taking a second or two in between calls to the function with the random number generator in it, it's fine. It's when it's getting called quickly that it's not changing. I understand that it's going by a clock, I believe the system clock, but it's the only random function I've used that at least gives the appearance of randomness. Is there some way I can get past this, or can someone recommend a random function that doesn't have this problem.

To be clear what I'm using, I've added a part of my code that uses the random function...

Expand|Select|Wrap|Line Numbers
  1.         .
  2.         .    
  3.     srand((unsigned)time(NULL)); 
  4.         .
  5.         .        
  6.     m_Value = (rand()%14)+1; 
  7.         .
  8.         .    
  9.  
Feb 20 '09 #1
4 12440
gpraghuram
1,275 Expert 1GB
if u want the random number to change everytime u call it then call seed() function befor calling rand and you will be able to achieve what u are looking for.


Raghu
Feb 20 '09 #2
donbock
2,426 Expert 2GB
Are you perhaps calling srand over and over again?
The usual practice is for a program to call srand exactly once and then call rand many times.
Feb 20 '09 #3
If you are calling srand() several times within a second then there is a chance that the seed value is not changing, and because you seed every time you intend to call rand() then then the random number sequence is repeated and will look like it does not change (fast enough). Accoring to the man page for time() the return value is in seconds so for example, if you call time() at 10 times per second the value time() will return will not change during the 10 calls until time advances by one second (does this make sense?). In fact, calling srand() before every call to rand() in the manner you are doing is killing the ability of rand() to produce random number sequences that are distinct from each other every time.

As mentioned before, you don't typically need to run srand() but once per session, however, if you do want to call srand() every frame and expect that rand() will produce a different sequence of random numbers each frame, then you need to seed the generator with a value that changes each frame (which time() does not when each frame is faster than a second). Not sure if this is desirable for your code but you could call srand with time(NULL) + a frame counter for example (a number that increments every frame producing a different seeding value). Or look at ftime(), it returns a structure where one of the members is the time since the epoch but in milliseconds. This number will change faster, although it still might not be fast enough for your code.

Good luck!
Feb 25 '09 #4
donbock
2,426 Expert 2GB
It is hard to generate a truly random sequence of numbers. You don't want to do this yourself -- some really heavy-duty math is involved. Trust the Standard C Library.

Calling srand with a new seed prior to each rand call means that you are essentially trying to write your own random number generator. Whether the result is truly random depends on the sequence of arguments you pass to srand, the under-the-hood guts of the srand implementation, and the under-the-hood guts of the rand implementation. At best you only know one of those. Unless you can prove otherwise, you have to assume that the result isn't truly random.

When your program starts, it should call srand once; and then never again. Call rand as often as you need a new random value. If you do this then you're using the Library the way it was meant to be used, giving you the full benefit of the math wizards who implemented rand.

It might be helpful during debugging if each run of the program yielded the same sequence of random values. If so, then pass a fixed constant value to srand.

Once you're done debugging you need to decide if your program would do its job better if each run had a unique sequence of random values. If so, then pass a hash value to srand that is likely to be different each time the program runs. System time is a simple hash; but you might feel motivated to do something more complicated that mixes together stuff like date, time, terminal id, and url.
Feb 26 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of...
3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
70
by: Ben Pfaff | last post by:
One issue that comes up fairly often around here is the poor quality of the pseudo-random number generators supplied with many C implementations. As a result, we have to recommend things like...
13
by: quickcur | last post by:
Suppose I have a function rand() that can generate one integer random number between 0 and 100. Suppose also rand() is very expensive. What is the fastest way to generate 10 different random number...
5
by: Peteroid | last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers. But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0 with resolution of a double (i.e., every possible...
10
by: Barry | last post by:
I am looking for the best way to produce a totally random number in VB.NET with no pre-defined sequence does anybody know if using Randomize followed by rnd is sufficient enough?
40
by: RadiationX | last post by:
I have a problem that I really don't understand at all. In my previous post I could get started on my projects I just had a few problems with syntax errors. This problem is something that I don't...
3
by: guy | last post by:
does anyone have good random number generator? the .NET one does not generate a wide enough range of values as i have to populate a psuedo array that has approx 500 million entries, using the .NET...
19
by: Sanchit | last post by:
I want to generate a randowm number between two numbers x and y i.e int randomNoBetween(int x, int y); Plz help Is there any such function??
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.