The random number generation time must be average.

Source: Internet
Author: User

Generate random arrays, which are used in development in many fields, such as game development. If the array generation time is fixed, the execution efficiency will be relatively high; if not fixed, it may affectProgram.

Question: generate 20 random numbers. The numbers in this group contain 2 groups, 1 group ~ An integer between 10, that is, two 1 and two 2. Therefore, each number is random.

 

Answer:

 Int  Num []  =     New     Int  [  20  ];
Int Index = 0 ; // Array subscript
Int Rannum; // Random Number
Int Time = 0 ; // Number of occurrences
Random R = New Random ();

While ( True ){
Rannum = R. nextint ( 9 ) + 1 ; // Random 1 ~ Number between 10

// Count the number of rannum occurrences
Time = 0 ;
For ( Int I = 0 ; I < Index; I ++ ){
If (Num [I] = Rannum ){
Time ++ ;
}
}

// If not twice
If (Time ! = 2 ){
Num [Index] = Rannum;
Index ++ ; // Continue assignment next
}
If (Index = Num. Length ){// Assign all values
Break ;
}

}

 

Answer B:

 //  Generate two groups of 1 ~ Rule numbers between 10  
Int Num [] = New Int [ 20 ];
For ( Int I = 0 ; I < Num. length; I ++ ){
Num [I] = I / 2 + 1 ;
}

// Random disruption of numerical order
Random R = New Random ();
Int Times = 30 ; // Number of exchanges
Int Ranindex; // Random subscript
Int Temp; // Exchange variable
Int CIndex; // Current subscript

For ( Int I = 0 ; I < Times; I ++ ){
Ranindex = R. nextint ( 20 ); // [0 ~ A random number between 20] As a subscript.

// Exchange with subscript I % 20
CIndex = I % 20 ;
If (CIndex ! = Ranindex ){
Temp = Num [cIndex];
Num [cIndex] = Num [ranindex];
Num [ranindex] = Temp;
}
}

Correct answer: B

Answer Analysis

Answer a uses natural thinking, that is, a random 1 ~ Between 10, and then determine whether the number appears twice in the array. If not twice, assign the number to the array and then randomly display the next number. Although this method can meet the requirements of the function, the execution efficiency of the program is unstable. The time required for each random process varies greatly, and may even be stuck. If the program generates more random numbers, this method cannot be used.

Answer B uses a work ing method, which includes a set of rule numbers and then randomly disconnects the order of the numbers. With this idea, the execution efficiency of the program is very stable and easy to implement. The procedure is as follows: generate two groups of 1 ~ The integer between 10, according ,...... In order to obtain a rule array num.

Then, the positions of numbers in the array are randomly exchanged by means of two-to-one exchange. a random number is generated as the subscript of an array exchange, and then exchanged with the current subscript of the array, after a certain number of exchanges, the numbers in the array become random. The time consumed by each switch is basically fixed,CodeThe execution efficiency is very stable, and the generation speed is also very efficient.

 

 

Random array in poker shuffling

Efficient generation of instant arrays.AlgorithmIt is widely used in actual program development, especially in games, such as shuffling in poker games and random Equipment Dropping in online games. Next, let's take a look at how a random array is efficiently generated in the program card.

The purpose of the code is to implement the shuffling function of a poker game. According to the characteristics of playing cards (each deck contains 54 different cards), each card in the playing card is numbered 0 ~ An array of All integers between 53, and shuffling is to convert the array of such a rule into a random array. The Code is as follows:

 //  Generate a deck numbered 0 ~ 53  
Int Poker [] = New Int [ 54 ];
For ( Int I = 0 ; I < Poker. length; I ++ ){
Poker [I] = I;
}

// Shuffling
Random R = New Random ();
Int Times = 54 ; // Number of exchanges
Int Ranindex; // Random subscript
Int Temp; // Exchange numbers
Int CIndex; // Current subscript

For ( Int I = 0 ; I < Times; I ++ ){
Ranindex = R. nextint ( 54 ); // [0 ~ Random Number between 53]
}

// Exchange with subscript I % 54
CIndex = I % 54 ;
If (CIndex ! = Ranindex ){
Temp = Poker [cIndex];
Poker [cIndex] = Poker [ranindex];
Poker [ranindex] = Temp;
}

Comments: in this code, a signature contains [0 ~ 53] rules array poker for All integers, and then exchange the elements in the array 54 times by means of two-to-two exchanges.

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.