Rene
2008-08-04 11:29:52 UTC
Hi to all!
When coding I stumbled on a peculiarity of the Randomize() function (with
capital 'R'!).
I made a very small test program (BCB6) to isolate this function, I just put
this piece of highly sophisticated code:
Randomize();
for (int teller = 0; teller < 10; teller++)
{
int a = random(1001);
int b = random(1001);
int c = random(1001);
int d = a + b; //this line is just here so I can watch c in the watch
windows as well.
}
in the constructor of the form of a visual application and stepped through
it. Every time I have the same numbers, starting with 774. When I call
randomize() instead of Randomize() everything is OK, I get different
sequences every time.
In an old console application I once wrote in which I had used the random
function to simulate a lot of user input, I had used the randomize()
function as well, no problem there. Today as a test I included vcl and use
Randomize() and there as well I got the same sequence all the time.
When I look in the help file, it says for the Randomize() function:
[helpfile]
Initializes the random number generator with a random value.
[snipped a bit here]
extern PACKAGE void __fastcall Randomize(void);
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.
[/helpfile]
From the fact that it is a void-void function I gather that I am not making
the error that I should be sending it some value and that the remark
"(obtained from the system clock)" means that this obtaining is done
automatically inside this function (besides, I would have gotten an error
then).
I have thought that I maybe should #include something else but wouldn't I
have been given an error message at compile time in that case?
I am puzzles, am I overlooking something or is this function faulty? Also,
what is the reason that there is a Randomize() function when there is
already a randomize() function?
Thank You very much in advance!
Yours sincerely,
Rene
When coding I stumbled on a peculiarity of the Randomize() function (with
capital 'R'!).
I made a very small test program (BCB6) to isolate this function, I just put
this piece of highly sophisticated code:
Randomize();
for (int teller = 0; teller < 10; teller++)
{
int a = random(1001);
int b = random(1001);
int c = random(1001);
int d = a + b; //this line is just here so I can watch c in the watch
windows as well.
}
in the constructor of the form of a visual application and stepped through
it. Every time I have the same numbers, starting with 774. When I call
randomize() instead of Randomize() everything is OK, I get different
sequences every time.
In an old console application I once wrote in which I had used the random
function to simulate a lot of user input, I had used the randomize()
function as well, no problem there. Today as a test I included vcl and use
Randomize() and there as well I got the same sequence all the time.
When I look in the help file, it says for the Randomize() function:
[helpfile]
Initializes the random number generator with a random value.
[snipped a bit here]
extern PACKAGE void __fastcall Randomize(void);
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.
[/helpfile]
From the fact that it is a void-void function I gather that I am not making
the error that I should be sending it some value and that the remark
"(obtained from the system clock)" means that this obtaining is done
automatically inside this function (besides, I would have gotten an error
then).
I have thought that I maybe should #include something else but wouldn't I
have been given an error message at compile time in that case?
I am puzzles, am I overlooking something or is this function faulty? Also,
what is the reason that there is a Randomize() function when there is
already a randomize() function?
Thank You very much in advance!
Yours sincerely,
Rene