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
> Every time I have the same numbers, starting with 774.
As you should. Randomize() is a VCL function, whereas random() is a C
library function. They do NOT share the same random number generator. Your
code is calling random() without initializing the C library's generator
beforehand.
> When I call randomize() instead of Randomize() everything is OK
Because you are now initializing the C library's generator.
> Today as a test I included vcl and use Randomize() and there
> as well I got the same sequence all the time.
Because Randomize() has nothing to do with random().
> When I look in the help file, it says for the Randomize() function:
[helpfile]
Initializes the *VCL's* random number generator with a random value.
Randomize initializes the *VCL's* built-in random number generator with
a random value (obtained from the system clock). The *VCL's* random number
generator should be initialized by making a call to Randomize, or by
assigning a value to RandSeed.
> am I overlooking something
Yes. You are mixing random number functions from two separate libraries
that have nothing to do with each other.
> is this function faulty?
No. You are simply using it wrong.
> Also, what is the reason that there is a Randomize() function when
> there is already a randomize() function?
Two separate libraries. The VCL is written in Delphi, not C/C++. There is
no C library available in Delphi, and thus no randomize() function.
Gambit
Thanks Chenzero, it does.
Yours sincerely,
Rene
Thank You as well, this clears things up. Is there a "plain" random function
in the VCL as well? Like Chenzero said, probably this VCL-random generator
is a.o. for the RandG() function, are there other functions that use this
VCL generator?
Yours sincerely,
Rene
> Thank You as well, this clears things up.
Gambit
In C++Builder 6, there is not "plain" random function in VCL.
but Random() is introduced in Delphi 7.
however, I believe that the same functionality can be implemented
by cbuilder 6.
yes, there are some functions use this VCL generator, please see
the help: random number routines
>
> Yours sincerely,
> Rene
>