Generating different sequences of random numbers

Posted on 2000-02-15
Last Modified: 2010-04-02
When I use randomize() and rand(), I get a sequence of random numbers in the range I need. However, I need to generate about a 1000 DIFFERENT sequences of random numbers in the given range. About 500 or so runs result in 500 same sequences since randomize() uses the system clock as a seed and 500 runs take less than a second to execute. In other words, since my program runs faster than the seed (clock seconds) can change, I get a lot of same sequences of random numbers. I tried slowing down the program by inserting loops but then it runs too slow. Is there anyway to get a 1000 different sequences?
Question by:mrmind3
LVL 22

Expert Comment

ID: 2524814
On a windows computer you could use GetTickCount as a seed.

Are you seeding the randome number more than once per run of the program?  it sounds like you are.  There is no need.  Just seed 1 time, when the program starts and you won't get any repeats.

Author Comment

ID: 2524898
But I do need to seed several times. I need to get  about a 1000 DIFFERENT sequences of random numbers not just one sequence.

Expert Comment

ID: 2525048
You could XOR all the generated random numbers, and use this as the next seed.
And perform some bit-swapping, or whatever, depending if the number is odd or even or so. This will for sure make 1000 different random sequences.
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

LVL 84

Expert Comment

ID: 2525087
randomize() once
then get 1000 different sequences of rand()

Expert Comment

ID: 2525098
This is what nietod also proposed, but mrmind3 wants to be able to start every sequence with a new seed.
LVL 22

Accepted Solution

nietod earned 50 total points
ID: 2525116
>> But I do need to seed several times.
>> I need to get  about a 1000 DIFFERENT
>> sequences of random
>> numbers not just one sequence.
what is?

 2 5 2 1 7 9 3 4

Is that a sequence of 8 random numbers?  Arre you sure it is not two sequences of 4 random numbers?  Perhaps it is 4 sequences of 2 random numbers

Just seed once, then generate the random numbers.  Where a sequence ends and another begins is arbitrary.  That is, it is up to you.  you don't have to use randomize() to start each sequence, just to start the process.

Expert Comment

ID: 2525129
I agree, but the second sequence of 4 numbers is affected by the first sequence, coz the rand() function is implemented in the way it has memory over the last generated numbers.
LVL 22

Expert Comment

ID: 2525283
If you want to be technical about it the numbers aren't random in either case.  But in either case the numbers will be evenly disrtibuted over the integer range and will display any easily detectable pattern.  So there is no difference in either case.  They are equally "random"

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C++ question 3 65
Create a path if not exists 7 77
Which Linux flavors will this run on? 6 87
Embarcadero C++ Builder XE10.1 Berlin red arrow Indicator 2 36
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question