Random none too random when called in quick succession

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.
Thanks in advance
Steve
StevenKAsked:
Who is Participating?
 
sfockConnect With a Mentor Commented:
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;
    memo1.lines.add(IntToStr(Random(100)));
  end;

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

  Randomize;
  for i := 0 to 100 do begin
    memo1.lines.add(IntToStr(Random(100)));
  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
 
kretzschmarCommented:
you could use a different randseed
0
 
FrankTheRatCommented:
You could call Randomize on each iteration, although I suspect this might have a performance impact.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
User137Commented:
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
 
User137Commented:
With RandG, the result was more random than with normal Random. And calling Randomize; only sets the RandSeed to systemClock (GetTickCount).
0
 
CleanupPingCommented:
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
All Courses

From novice to tech pro — start learning today.