Random none too random when called in quick succession

Posted on 2003-02-19
Medium Priority
Last Modified: 2010-04-04
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
Question by:StevenK
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

Expert Comment

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

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

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

  for i := 0 to 100 do begin

in my little test the first usage gives results exactly as u discribed, the 2nd one makes what we all expect of Ransom ;-)
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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.

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).

Expert Comment

ID: 9317184
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 
Post your closing recommendations!  No comment means you don't care.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.

Join & Ask a Question