Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Behaviour of Randomize() function that I don't understand

14 views
Skip to first unread message

Rene

unread,
Aug 4, 2008, 7:29:52 AM8/4/08
to
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

unread,
Aug 4, 2008, 11:26:23 AM8/4/08
to
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.


Remy Lebeau (TeamB)

unread,
Aug 4, 2008, 1:08:38 PM8/4/08
to

"Rene" <my....@is.nobody> wrote in message
news:4896e8d2$1...@newsgroups.borland.com...

> 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


Rene

unread,
Aug 4, 2008, 2:08:54 PM8/4/08
to
"chenzero" <some...@earth.solar.com> schreef in bericht
news:4897...@newsgroups.borland.com...

Thanks Chenzero, it does.

Yours sincerely,
Rene


Rene

unread,
Aug 4, 2008, 2:11:10 PM8/4/08
to
"Remy Lebeau (TeamB)" <no....@no.spam.com> schreef in bericht
news:489737ff$3...@newsgroups.borland.com...

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)

unread,
Aug 4, 2008, 2:57:30 PM8/4/08
to

"Rene" <inv...@invalid.com> wrote in message
news:489746a1$1...@newsgroups.borland.com...

> Thank You as well, this clears things up.


Gambit


chenzero

unread,
Aug 5, 2008, 11:12:01 AM8/5/08
to

"Rene" <inv...@invalid.com> 写入消息新闻:489746a1$1...@newsgroups.borland.com...
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

>
> Yours sincerely,
> Rene
>

Thomas Maeder [TeamB]

unread,
Aug 5, 2008, 11:27:10 AM8/5/08
to

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!
0 new messages