Solved

Random

Posted on 2000-04-05
9
299 Views
Last Modified: 2010-04-04
I need to create random numbers.

the output of

for n := 0 to 100 do
begin
   Randomize;
   x := Random ( 50 );
   TMemo.Lines.Add ( IntToStr ( x ) );
end;

often looks like this:

5
5
5
5
5
5
33
33
33

how to avoid this and generate "real" random numbers?

like this:

34
7
29
44
2
17

By the way: I'm running a PIII500Mhz,
so I thought of the machine being too fast (Randomize has something to do with the system time) so I added some
Sleep ( Random (20) * Random (20));
Lines, but still no help.

A "Sleep ( 3000 );" does help, but makes it realy slow!

Regards, Oli
0
Comment
Question by:Oli2
[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
9 Comments
 
LVL 3

Accepted Solution

by:
ckaneta earned 100 total points
ID: 2687758
put randomize outside of the loop
0
 
LVL 1

Author Comment

by:Oli2
ID: 2687765
Edited text of question.
0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2687766
Move Randomize outside of the for loop:

var n,x:integer;
begin
   Randomize;

for n := 0 to 100 do
begin
   x := Random ( 50 );
   Listbox1.items.Add ( IntToStr ( x ) );
end;


end;


Good luck!!


0
Industry Leaders: 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!

 
LVL 3

Expert Comment

by:hagur
ID: 2687769
Try this:

for n := 0 to 100 do
begin
   Randomize;
   x := Random ( 50 );
   Memo1.Lines.Add ( IntToStr ( x ) );
   Sleep(100);
end;

or even

for n := 0 to 100 do
begin
   Randomize;
   x := Random ( 50 );
   Memo1.Lines.Add ( IntToStr ( x ) );
   Sleep(500);
end;

This works perfectly for me, although on a slower machine, but hey ... a delay is a delay :)

-H
0
 
LVL 1

Author Comment

by:Oli2
ID: 2687770
Hi, ckaneta!

It seems you're right!!!
How come this behaviour?
Can you explain it?
(just being curious)

Regards, Oli
0
 
LVL 17

Expert Comment

by:inthe
ID: 2687791
from delphi help:(see last paragraph)

Initializes the random number generator with a random value.

Unit

System

Category

random number routines

procedure Randomize;

Description

Randomize initializes the built-in random number generator with a random value (obtained from the system clock). The random number generator should be initialized by making a call to Randomize, or by assigning a value to RandSeed.

Do not combine the call to Randomize in a loop with calls to the Random function. Typically, Randomize is called only once, before all calls to Random.
0
 
LVL 1

Author Comment

by:Oli2
ID: 2687808
hmm, okay.
Now my problem: who to give the points to?
Suggestions?
I think of giving them to ckaneta - he was the first...
Is that okay with everybody?

Oli
0
 
LVL 1

Expert Comment

by:umulig
ID: 2689047
ckaneta was first he should get the points.

Umulig
0
 
LVL 1

Expert Comment

by:sageryd
ID: 2689350
Agree! ckaneta was first. Does anyone have an idea on how the random function works? Is there any documentation about it somewhere? You don't have to answer asthough it wasn't I that put up the original Q.
0

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month6 days, 3 hours left to enroll

627 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