Home > Backend Development > Golang > golang rand does not repeat

golang rand does not repeat

WBOY
Release: 2023-05-09 22:35:36
Original
935 people have browsed it

Random numbers are often used in programming, and Golang provides the rand package to generate pseudo-random numbers. However, in some cases we may need to generate a set of random numbers that are different from each other. In order to achieve this goal, we need to consider a feasible algorithm.

This article will introduce several methods to generate non-repeating random numbers, and provide a method to use the Golang rand package to generate non-repeating random numbers.

Method 1: Shuffling Algorithm

In the shuffling algorithm, we randomly arrange an array of numbers with the same range once. The time complexity of this algorithm is O(n). The disadvantage of this method is that the system takes up a lot of memory when we use this algorithm to generate a large number of random numbers.

The algorithm is implemented as follows:

  1. Generate an array of numbers with a range.
  2. Generate a random number in a range and swap that number with another number in the range.
  3. Repeat step 2 until all the unique numbers required have been generated.

Method 2: Rejection Sampling Algorithm

In the rejection sampling algorithm, we store the generated random numbers in a set and check the set when generating the next random number whether the same number already exists in . If present, regenerate the random number. The advantage of this algorithm is that it is a space efficient algorithm. However, this algorithm can become more time-consuming when the number of unique numbers we need to generate is larger.

The algorithm is implemented as follows:

  1. Initialize a numerical range, a target number of elements and a result array.
  2. Generate a random number within the range of values ​​and add the random number to the result array.
  3. If the number of numbers contained in the result array is less than the target number, repeat step 2.

Method Three: Bitmap Algorithm

In the bitmap algorithm, we create a bitmap where each bit represents the presence or absence of a number in the range. We use this bitmap to keep track of whether a certain number already exists in the set of random numbers we have generated. The advantage of this algorithm is that it is a time- and space-efficient algorithm.

The algorithm is implemented as follows:

  1. Initialize a numerical range, a target number of elements and a result array.
  2. Create a bitmap where each element represents a number within a range.
  3. Randomly generate a number and check whether the bitmap element corresponding to the number is zero. If zero, it means that the number has not been generated; otherwise, it means that the number has been generated. If a bit on the bitmap is equal to zero then it is set to one.
  4. Add the generated unique numbers to the result array. If the resulting array contains less than the target number of numbers, repeat step 3.

Method 4: Golang’s rand package implementation

Golang provides the rand package, which can be used to generate pseudo-random numbers. We can use this to generate non-repeating random numbers.

The algorithm is implemented as follows:

  1. Initialize a numerical range and a target number of elements.
  2. Use Golang's rand package to generate a random number and save the first element in the result array.
  3. Store numbers in a numeric range in a slice.
  4. Find the number in the resulting array in the slice and delete it. This can be achieved using index operations in slices.
  5. Repeat steps 2 and 4 until the resulting array contains the required number of numbers.

The above four methods can be used to generate non-repeating random numbers. Each algorithm has its advantages and disadvantages. Which algorithm to use depends on your needs, if the importance lies in space then use rejection sampling algorithm, if in importance over time then choose bitmap algorithm. And if you are using Golang, you can directly use the rand package to implement a simple, direct, and efficient non-repeating random number generation algorithm.

The above is the detailed content of golang rand does not repeat. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template