A Simple Algorithm for Delphi to generate random numbers without duplicates

Source: Internet
Author: User
Algorithms that generate non-repeated random numbers can solve problems such as random shuffling and lottery. Some algorithms on the Internet are more conscious and traditional, and simple algorithms are now provided. The following source program contains 36 numbers, which are randomly arranged to generate random numbers that are not repeated.

VaR

AA: array [1 .. 36] of string [2]; // The AA array is an array to be randomly arranged,

{Assign values to the AA array in sequence for 1 .. 36 First}

Procedure tform1.formcreate (Sender: tobject );

VaR

I: integer;

Begin

For I: = 1 to 36 do begin

AA [I]: = inttostr (I );

Bb [I]: = inttostr (I );

End;

End;

{Random sorting}

Procedure tform1.button1click (Sender: tobject );

VaR

I, J: integer;

Swapa: String [2];

Begin

Randomize;

For I: = 1 to 36 do begin

J: = 1 + random (36 );

Swapa: = AA [I];

AA [I]: = AA [J];

AA [J]: = swapa;

End; // random numbers that are not repeated

 

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.