Discussion:
Behaviour of Randomize() function that I don't understand
(too old to reply)
Rene
2008-08-04 11:29:52 UTC
Permalink
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
chenzero
2008-08-04 15:26:23 UTC
Permalink
On my opinion, one of the reason to introduce Randomize() is to
support VCL expand random functions, e.g.
RandG() - Generates random numbers with Gaussian distribution.
internally, they use a different random seed with randomize().
so, to use random(), I think it should use randomize()
Hope this help.
Rene
2008-08-04 18:08:54 UTC
Permalink
Post by chenzero
On my opinion, one of the reason to introduce Randomize() is to
support VCL expand random functions, e.g.
RandG() - Generates random numbers with Gaussian distribution.
internally, they use a different random seed with randomize().
so, to use random(), I think it should use randomize()
Hope this help.
Thanks Chenzero, it does.

Yours sincerely,
Rene
Remy Lebeau (TeamB)
2008-08-04 17:08:38 UTC
Permalink
Post by 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.
Post by Rene
When I call randomize() instead of Randomize() everything is OK
Because you are now initializing the C library's generator.
Post by Rene
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().
[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.
Post by Rene
am I overlooking something
Yes. You are mixing random number functions from two separate libraries
that have nothing to do with each other.
Post by Rene
is this function faulty?
No. You are simply using it wrong.
Post by Rene
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
Rene
2008-08-04 18:11:10 UTC
Permalink
Post by Remy Lebeau (TeamB)
Post by 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.
Post by Rene
When I call randomize() instead of Randomize() everything is OK
Because you are now initializing the C library's generator.
Post by Rene
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().
[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.
Post by Rene
am I overlooking something
Yes. You are mixing random number functions from two separate libraries
that have nothing to do with each other.
Post by Rene
is this function faulty?
No. You are simply using it wrong.
Post by Rene
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.
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
Remy Lebeau (TeamB)
2008-08-04 18:57:30 UTC
Permalink
Post by Rene
Thank You as well, this clears things up.
Gambit
Thomas Maeder [TeamB]
2008-08-05 15:27:10 UTC
Permalink
Please direct your browser at http://www.teamb.com/newsgroups and
read the newsgroup guidelines. One of them asks us not to quote entire
posts we are following up to; instead, please trim the quotes to the
parts relevant for your reply. Thanks!
chenzero
2008-08-05 15:12:01 UTC
Permalink
Post by Rene
Post by Remy Lebeau (TeamB)
Post by 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.
Post by Rene
When I call randomize() instead of Randomize() everything is OK
Because you are now initializing the C library's generator.
Post by Rene
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().
[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.
Post by Rene
am I overlooking something
Yes. You are mixing random number functions from two separate libraries
that have nothing to do with each other.
Post by Rene
is this function faulty?
No. You are simply using it wrong.
Post by Rene
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.
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?
I am regretable that make you confused :).yes, the reason to introduce
Randomize() is like what Remy said.

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
Post by Rene
Yours sincerely,
Rene
Loading...