Solved

# Random none too random when called in quick succession

Posted on 2003-02-19
Medium Priority
138 Views
I'm making use of the 'Randomize' and 'Random()' calls.
However due to the nature of my code (generating a random number as part of a finite looping) the number returned appears to be constant.
If I loop for 10 iterations then the value returned by the 'Random' call is pretty constant.
If I extend the loop to something like 100 then I get similar blocks, ie. the first 20 returns are the same, the 21st differs but then matches returns 22 through 40, etc.
Reviewing the Delphi help files it indicates that the Random function makes use of a value obtained from the system clock - clearly this will remain pretty much a constant on a high-spec machine doing a dozen swift iterations.
How can I best work around this such that I get a truly random return value for each iteration in the loop ?
Any help would be appreciated.
Steve
0
Question by:StevenK
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions

LVL 27

Expert Comment

ID: 7982889
you could use a different randseed
0

LVL 1

Expert Comment

ID: 7983029
You could call Randomize on each iteration, although I suspect this might have a performance impact.
0

LVL 3

Accepted Solution

sfock earned 150 total points
ID: 7983345
well i have to guess your code but I'd say your loop looks kind of this :

for i := 0 to 100 do begin
Randomize;
end;

That is a wrong usage u should use randomize only once like this:

Randomize;
for i := 0 to 100 do begin
end;

in my little test the first usage gives results exactly as u discribed, the 2nd one makes what we all expect of Ransom ;-)
0

LVL 1

Expert Comment

ID: 7985845
I must say, that when i was making graph-making program, i crossed the same problem.
Changing the RandSeed won't help. Using RandG, makes too similar patterns, that on 3D-graph can be seen as waves.
I think the solution would be different algorithm.
0

LVL 1

Expert Comment

ID: 7985849
With RandG, the result was more random than with normal Random. And calling Randomize; only sets the RandSeed to systemClock (GetTickCount).
0

Expert Comment

ID: 9317184
StevenK:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

## Featured Post

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
###### Suggested Courses
Course of the Month14 days, 12 hours left to enroll

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

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