Oli2
asked on
Random
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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!!
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!!
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
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
ASKER
Hi, ckaneta!
It seems you're right!!!
How come this behaviour?
Can you explain it?
(just being curious)
Regards, Oli
It seems you're right!!!
How come this behaviour?
Can you explain it?
(just being curious)
Regards, Oli
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.
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.
ASKER
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
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
ckaneta was first he should get the points.
Umulig
Umulig
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.
ASKER