Randomize & Random

Source: Internet
Author: User

I saw this code when I learned the ProcessMessage function. I didn't use the Randomize function, so I learned it by the way;

Procedure TForm1.Button1Click (Sender: TObject); </p> <p> var <br/> I, J, X, Y: Word; <br/> begin <br/> I: = 0; <br/> J: = 0; <br/> while I <64000 do <br/> begin <br/> Randomize; <br/> while J <64000 do <br/> begin <br/> Y: = Random (J); <br/> Inc (J); <br/> end; <br/> X: = Random (I ); <br/> Inc (I); <br/> end; <br/> Canvas. textOut (10, 10, 'the Button1Click handler is finished'); </p> <p> end;

The original description of this function in help is as follows:

Initializes the random number generator with a random value.

The previous Initializes the random number generator should be translated into: it should be correct to initialize the random number generator, but with a random value how to translate it with a random value? Next, let's look at Y: = Random (J); will it be related to this function? Continue to query the random () function:

Generates random numbers within a specified range. // the random number is generated within the specified range.

Delphi syntax:

Function Random [(Range: Integer)];

More details are provided below:

In Delphi code, Random returns a random number within the range 0 <= X <Range. If Range is not specified, the result is a real-type random number within the range

0 <= X <1.

 

At this point, I still cannot see the relationship between the two functions. Let's take a look at Randomize's description:

Description

Randomize initializes the built-in random number generator with a random value (obtained from the system clock ). the random number generator shoshould be initialized by making a call to Randomize, or by assigning a value to RandSeed.

{Randomize uses a random value to initialize the built-in instant number generator (the random value comes from the system clock ). The number generator calls the Randomize function or specifies the RandSeed variable for initialization .}

Do not combine the call to Randomize in a loop with callto the Random function. Typically, Randomize is called only once, before all callto Random.

{Do not call the Randomize and Random functions at the same layer. A typical application is to call Randomize once before all the random functions}

 

In my understanding, the generation of numbers changes according to the system clock. randomize is used to hook the built-in random number generator with the system clock, the next random function will take effect.

 

I can't do it by theory. Let's write an example:

Procedure tform1.bt _ 3 click (Sender: tobject); <br/> var <br/> I: integer; <br/> begin <br/> // randomize; <br/> memo_1.lines.clear; <br/> for I: = 1 to 100 do <br/> memo_1.lines.add (inttostr (random (100); <br/> end;

Log out of randomize and run it. It's okay, so it changes!
Remove logout, run, and change!
What's going on?
Put it under d5. It's still the same!
What's going on?
After randomize is removed from the network, the values of the number of run times are the same, rather than the data changes during a run. Take a closer look.
At this point, I have understood these two functions.
I understand it, but I used to write Random directly without randomize! Khan! Fortunately, the repetition rate of that data is almost 0, so it is not too troublesome.
In the future, I will read more books and read other people's code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.