C # shuffling Algorithm

Source: Internet
Author: User

Let's take a look at card games first. A card is composed of 52 different cards. It must generate non-repeated cards during licensing and the shuffling process must be fair, that is, 52! The order of cards should be equal to the probability. Obviously, the random numbers produced by this random arrangement must be evenly distributed and independent. The Code is as follows:

  1. Using System;
  2. Using System. Diagnostics;
  3.  
  4. Namespace Lucifer. CSharp. Sample
  5. {
  6. Class Program
  7. {
  8. Static void Main (string [] args)
  9. {
  10. // Initialize the Board
  11. Int [] array = new int [52];
  12. For (int I = 0; I <array. Length; I ++)
  13. {
  14. Array = I;
  15. }
  16.  
  17. // Shuffling
  18. Permute <int> (array );
  19.  
  20. // Verify the Board
  21. For (int I = 0; I <array. Length; I ++)
  22. {
  23. Var value = array;
  24. For (int j = 0; j <array. Length; j ++)
  25. {
  26. If (j = I) continue;
  27. Debug. Assert (array [j]! = Value );
  28. }
  29. }
  30. }
  31.  
  32. Static void Permute <T> (T [] array)
  33. {
  34. Random random = new Random ();
  35. For (int I = 1; I <array. Length; I ++)
  36. {
  37. Swap <T> (array, I, random. Next (0, I ));
  38. }
  39. }
  40.  
  41. Static void Swap <T> (T [] array, int indexA, int indexB)
  42. {
  43. T temp = array [indexA];
  44. Array [indexA] = array [indexB];
  45. Array [indexB] = temp;
  46. }
  47. }
  48. }
Copy code

In the sample code, the Permute <T> (T [] array) method generates a random sequence. The first loop uses 1, 2, 3 ,..., N initializes the sequence. The second loop completes a random shuffling. In each iteration of the loop, the value of array [j] is exchanged (or may not be exchanged) between an element in the range [0, j] Where the array is located ).

However, do we have to ask if the Permute <T> (T [] array) method produces all the permutation probabilities?

Based on this algorithm, the answer is yes. Because there are N in total! Possible arrangement, while Swap <T> (array, I, random. Next (0, I); the different results of the Next method called by the N-1 are N! Type. However, in fact, the answer is no. Not all permutation is equal probability. The problem lies in the cute Pseudo-Random Number Generator (Pseudo-Random Number Generator. The randomness of the PRNG largely limits the randomness of the random sequence. Therefore, the above Code requires a better pseudo-random number generator to make the actual situation more consistent with the theory. However, good PRNG performance often decreases, such as Mt19937 randomization algorithm.

Related Article

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.