Method, apparatus, electronic device, and storage medium for generating random numbers
By using random prime seeds and target random coefficients for calculation of the balance in the random number generation round, the problems of long generation time and large storage space of traditional random number generators are solved, and an efficient and storage-saving random number generation method is realized.
Patent Information
- Application Number
- CN202510350521.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-24
- Publication Date
- 2025-06-27
- Estimated Expiration
- 2045-03-24
AI Technical Summary
The traditional non-repetitive random number generator needs to be compared with historical random numbers one by one, resulting in the generation time of random numbers that take up too long and occupy a large storage space.
By responding to the random number generation instruction, we determine the random number generation round, dividend and random prime seed, and calculate the balance based on the target product and dividend of the random prime seed and the target random coefficient for each round of random number generation round, and obtain the target remainder, thereby determining the target random number.
This method can directly obtain target random numbers that are different from each other through multiple rounds of random number generation, which reduces the time required for the random number generation process, improves the efficiency of random number generation, and saves storage space.
Smart Images

Figure CN119861903B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present invention relate to the field of computer technologies, and particularly to a method, apparatus, electronic device, and storage medium for generating random numbers. Background Art
[0002] With the rapid development of information technologies, the demand for high-quality random numbers in the fields of data processing and information security is increasing day by day. A traditional non-repeating random number generator can sequentially output multiple non-repeating random numbers within a specified range. For example, if the random number generation range is set to 0 - 100, the non-repeating random number generator will randomly output 100 values in sequence, and these 100 values are different from each other and within the range of 0 - 100.
[0003] In related technologies, the generation of random numbers is implemented based on a random number generator and a list. The list is used to store historical random numbers that have been generated and output. When generating a new random number R each time, it is necessary to sequentially compare it with the historical random numbers in the list to determine whether it is repeated with the historical random numbers. If the random number R is not included in the list, then R is added to the list and output as a random number; if the list contains the random number R, then a new random number is regenerated and compared again. Such a random number generation method requires comparing each generated random number with historical random numbers one by one each time to ensure the non-repetitiveness and randomness of random numbers, resulting in excessive time spent on repeatedly generating random numbers, and storing historical random numbers through a list requires a large amount of storage space. Summary of the Invention
[0004] Embodiments of the present invention provide a method, apparatus, electronic device, and storage medium for generating random numbers.
[0005] To solve the above problems, embodiments of the present invention disclose a method for generating random numbers, the method including:
[0006] Responding to a random number generation instruction, based on the random number generation instruction, determining the number of random number generation rounds, the dividend, and the random prime number seed;
[0007] For any random number generation round, based on the target product and the dividend, determining the target remainder corresponding to the random number generation round; the target product is obtained based on the random prime number seed and the target random coefficient corresponding to the random number generation round, and the target random coefficients corresponding to different random number generation rounds are different;
[0008] When the number of random number generation rounds is reached, based on the target remainders corresponding to multiple random number generation rounds, determining multiple target random numbers.
[0009] On the other hand, an embodiment of the present invention discloses a random number generation device, which includes:
[0010] A first determination module, configured to, in response to a random number generation instruction, determine a random number generation round number, a dividend, and a random prime number seed based on the random number generation instruction;
[0011] A second determination module, configured to, for any random number generation round, determine a target remainder corresponding to the random number generation round based on a target product and the dividend; the target product is obtained based on the random prime number seed and a target random coefficient corresponding to the random number generation round, and the target random coefficients corresponding to different random number generation rounds are different;
[0012] A third determination module, configured to, when the random number generation round number is reached, determine a plurality of target random numbers based on the target remainders corresponding to the plurality of random number generation rounds.
[0013] In another aspect, an embodiment of the present invention discloses an electronic device, including: a processor, a memory, a communication interface, and a communication bus. The processor, the memory, and the communication interface complete communication with each other through the communication bus; the memory is used to store executable instructions, and the executable instructions cause the processor to execute the foregoing method.
[0014] An embodiment of the present invention also discloses a readable storage medium, on which executable instructions are stored. When executed by one or more processors, the executable instructions cause the processor to execute the method as described above.
[0015] The embodiments of the present invention have the following advantages: In the random number generation method provided in the embodiments of the present invention, when a random number generation instruction is received, based on the random number generation instruction, the number of random number generation rounds, the dividend, and the random prime number seed are determined; for any random number generation round, based on the target product of the random prime number seed and the target random coefficient corresponding to the current random number generation round and the dividend, the target remainder corresponding to the current random number generation round is determined; the target random coefficients corresponding to different random number generation rounds are different; when the number of random number generation rounds is reached, based on the target remainders corresponding to multiple random number generation rounds, multiple target random numbers are determined. In this way, by performing a remainder calculation based on the target product of the random prime number seed and the target random coefficient corresponding to the current random number generation round and the dividend in each random number generation round, and then determining the target random number based on the obtained target remainder, it can be ensured that the target remainders obtained in different random number generation rounds are different from each other, and further ensure that multiple target random numbers are different from each other. Since the embodiments of the present invention can directly obtain different target random numbers through multiple random number generation rounds, there is no need to compare with historical random numbers one by one to determine whether the random number is repeated, which reduces the time required for the random number generation process and improves the random number generation efficiency. At the same time, there is no need to set up a list to store historical random numbers to ensure randomness. The embodiments of the present invention save storage space while ensuring the randomness of random numbers. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following will briefly introduce the drawings required for the description of the embodiments of the present invention. Obviously, the following drawings are only some embodiments of the present invention. For those of ordinary skill in the art, other drawings can be obtained based on these drawings without creative efforts.
[0017] Figure 1 is a flowchart of the steps of a random number generation method provided by an embodiment of the present invention;
[0018] Figure 2 is a block diagram of a random number generation device provided by an embodiment of the present invention;
[0019] Figure 3 is a schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0020] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
[0021] Referring to Figure 1 , a step flowchart of a method for generating a random number provided by an embodiment of the present invention is shown. As Figure 1 shown, the method may specifically include the following steps:
[0022] Step 101, in response to a random number generation instruction, based on the random number generation instruction, determine the random number generation rounds, the dividend, and the random prime number seed.
[0023] In an embodiment of the present invention, the random number generator, in response to a random number generation instruction, parses the random number generation instruction and determines the random number generation rounds, the dividend, and the random prime number seed indicated by the random number generation instruction based on the random number generation instruction. Specifically, the random number generation instruction sent to the random number generator may directly include the random number generation rounds, the dividend, and the random prime number seed, or may include the random number generation quantity and the target random range. In this case, the random number generation rounds and the dividend may be determined based on the random number generation quantity and the target random range, and a random prime number seed is randomly generated. The information included in the random number generation instruction may be set according to actual needs.
[0024] Among them, the random number generation rounds are used to represent the number of random number generation rounds to be performed in the random number generation process. The random prime number seed is a transformation coefficient that needs to be used in the random number generation process. The random prime number seed may be any randomly generated prime number. A prime number (also known as a prime) refers to a natural number greater than 1 that has no other factors except 1 and itself.
[0025] Optionally, an embodiment of the present invention may further include the following steps:
[0026] Step 201, based on the random number generation quantity and the target random range in the random number generation requirement, determine the random number generation rounds and the dividend.
[0027] In an embodiment of the present invention, the processor obtains a random number generation requirement. The random number generation requirement can be obtained by receiving an external input or a requirement that matches the current service progress according to the current service progress. The random number generation requirement can be determined according to different application scenarios. Different application scenarios include, for example, out-of-order transmission in data communication, that is, the sender sends a group of randomly out-of-order data to the receiver, and the receiver then restores it to the original sequence to achieve an encryption effect; random traversal of RAM self-check, that is, the entire RAM area is divided into multiple self-checks, and non-repeating random addresses are generated sequentially at regular intervals, and only the content of that address is checked. Since the addresses are not repeated, the self-check of all areas will eventually be completed.
[0028] The random number generation requirement may include the number of random numbers to be generated, the target random range, and the data type (such as integer, floating point number), etc. According to the number of random numbers to be generated and the target random range in the random number generation requirement, the number of random number generation rounds and the dividend are determined. The target random range is used to define the limited range of random numbers during the random number generation process, that is, only one value can be randomly generated within the target random range during the random number generation process. The target random range includes the upper limit of the random value and the lower limit of the random value. For example, if the target random range is 0-10, the upper limit of the random value is 10 and the lower limit of the random value is 0.
[0029] Determine the number of random number generation rounds based on the number of random numbers to be generated, and determine the dividend based on the target random range.
[0030] Optionally, step 201 may include the following steps:
[0031] Step 301: Analyze the random number generation requirement to obtain the number of random numbers to be generated and the target random range; the number of random numbers to be generated is less than or equal to the target number of values included in the target random range.
[0032] In an embodiment of the present invention, the random number generation requirement is analyzed to obtain the number of random numbers to be generated and the target random range. To ensure the non-repetitiveness of random numbers, the number of random numbers to be generated is less than or equal to the target number of values included in the target random range. The target random range can be composed of the upper limit of the random value and the lower limit of the random value. The target number of values included in the target random range can be the number of integers included between the upper limit of the random value and the lower limit of the random value. Exemplarily, if the target random range is 0-3, the values included between 0-3 are 4, namely 0, 1, 2, 3, that is, the target number is 4, then the number of random numbers to be generated is less than or equal to 4.
[0033] Exemplarily, the number of random numbers generated in the random number generation requirement can be parsed. If the number is directly specified in the random number generation requirement, record this value as the number of random numbers generated. If the number is dependent on certain conditions or parameters (such as system status), then calculate the number of random numbers generated dynamically according to these conditions and parameters. Parse the numerical range of random numbers in the random number generation requirement, which usually includes an upper limit of the random number value and a lower limit of the random number value, defining a closed interval of the random number generation range.
[0034] Step 302: Determine the number of random number generations as the number of random number generation rounds.
[0035] In the embodiments of the present invention, each random number generation round is used to generate a target random number. Therefore, the number of random numbers generated is equal to the number of random number generation rounds, that is, the number of rounds of random number generation is equal to the number of random numbers to be generated.
[0036] Step 303: Determine the dividend based on the target quantity.
[0037] In the embodiments of the present invention, the target quantity of the values included between the upper limit of the random number value and the lower limit of the random number value can be determined as the dividend. Exemplarily, assuming the target random range is 0 - 3, the dividend can be 4; if the target random range is 2 - 6, the dividend can be 5.
[0038] In the embodiments of the present invention, according to the number of random numbers generated in the random number generation requirement and the target random range, the dividend and the number of random number generation rounds are flexibly determined, improving the determination efficiency of the number of random number generation rounds and the dividend while meeting the random number generation requirement.
[0039] Step 202: Randomly generate the random prime number seed.
[0040] In the embodiments of the present invention, a prime number list can be preset in advance, and the prime number list can contain multiple prime numbers. Exemplarily, the Sieve of Eratosthenes can be used to find prime numbers among natural numbers, and a prime number list is formed based on these prime numbers. Randomly select a prime number from the prime number list as the random prime number seed.
[0041] Optionally, step 202 may include the following steps:
[0042] Step 401: Determine half of the dividend as the lower limit of random seed generation, and determine twice the dividend as the upper limit of random seed generation.
[0043] In the embodiments of the present invention, the selected random prime number seed will affect the apparent randomness of the result. To obtain a better random effect, it is set that the order of magnitude of the selected random prime number seed is similar to that of the dividend. For example, it can be between N / 2 and N*2 (where N is the dividend).
[0044] Determine half of the dividend as the lower limit for generating the random seed, and, determine twice the dividend as the upper limit for generating the random seed. Take the closed interval between the lower limit for generating the random seed and the upper limit for generating the random seed as the random selection range of the random prime number seed.
[0045] Step 402: Randomly select a prime number between the upper limit for generating the random seed and the lower limit for generating the random seed as the random prime number seed.
[0046] In the embodiments of the present invention, randomly select a prime number between the upper limit for generating the random seed and the lower limit for generating the random seed as the random prime number seed. Specifically, a first prime number list corresponding to the selection range defined by the upper limit for generating the random seed and the lower limit for generating the random seed can be determined, and a prime number is randomly selected from the first prime number list as the random prime number seed.
[0047] In the embodiments of the present invention, determining half of the dividend as the lower limit for generating the random seed and, determining twice the dividend as the upper limit for generating the random seed can improve the random effect of the target random number and ensure the randomness of the target random number.
[0048] Step 203: Generate the random number generation instruction based on the random number generation rounds, the dividend, and the random prime number seed.
[0049] In the embodiments of the present invention, generate a random number generation instruction based on the random number generation rounds, the dividend, and the random prime number seed. The random number generation instruction can be an instruction generated internally by the random number generator or an instruction received from the outside. Exemplarily, the processor can send the random number generation instruction to the random number generator. When the random number generator receives the random number generation instruction, it will parse the random number generation instruction to obtain the random number generation rounds, the dividend, and the random prime number seed, and then generate random numbers based on the random number generation rounds, the dividend, and the random prime number seed. The random number generation instruction can also be generated internally by the random number generator. After generating the random number generation instruction, the random number generator will directly respond to the random number generation instruction, parse the random number generation instruction, and then generate random numbers based on the random number generation rounds, the dividend, and the random prime number seed. Exemplarily, a random number generation function in a programming language or tool library can be used to generate a random number generation instruction based on the random number generation rounds, the dividend, and the random prime number seed.
[0050] In an embodiment of the present invention, based on the requirement for generating a random number, a random number generation instruction is generated, which can ensure that the generated random number meets the requirements of a specific application scenario, thereby improving the accuracy and effectiveness of the random number in different application scenarios.
[0051] Step 102: For any random number generation round, determine the target remainder corresponding to the random number generation round based on the target product and the dividend; the target product is obtained based on the random prime number seed and the target random coefficient corresponding to the random number generation round, and the target random coefficients corresponding to different random number generation rounds are different.
[0052] In an embodiment of the present invention, according to the congruence theorem "Given a positive integer m, if two integers a and b satisfy that a - b can be divided by m, that is, (a - b) / m gives an integer, then it is said that the integers a and b are congruent modulo m, denoted as a ≡ b (mod m)". It can be known that the necessary and sufficient condition for a and b to be congruent is that a - b can be divided by m, that is, (m|a - b). Among them, when two integers a and b are divided by the same integer m, if they get the same remainder, they are called congruent. According to the above theorem, it can be inferred that Corollary 1: If a - b cannot be divided by m, then the remainders of a and b divided by m are different. Further based on Corollary 1, it can be obtained that: Take any prime number p and the specified interval 0 - N, and successively take p, 2*p, 3*p,... N*p, and their remainders when divided by N are all different. Specifically, since p is a prime number, p cannot be divided by N. Therefore, 2*p - p = p, 3*p - 2*p = p... N*p - (N - 1)*p = p (equivalent to a - b) cannot be divided by N (equivalent to m), so p, 2*p, 3*p,... N*p, and their remainders when divided by N are all different.
[0053] Exemplarily, assume p = 7 and N = 10, then: 7 % 10 = 7; 14 % 10 = 4; 21 % 10 = 1; 28 % 10 = 8; 35 % 10 = 5; 42 % 10 = 2; 49 % 10 = 9; 56 % 10 = 6; 63 % 10 = 3; 70 % 10 = 0. It can be seen that the remainders are all different.
[0054] Based on the above corollary, the target remainder corresponding to the current random number generation round can be determined by performing a remainder calculation based on the target product of the random prime number seed and the target coefficient corresponding to the current random number generation round, and the dividend, so that the target remainders corresponding to different random number generation rounds are different from each other.
[0055] Among them, the target random coefficients corresponding to different random number generation rounds are different. The target random coefficient corresponding to a random number generation round is determined based on the target random coefficient corresponding to the previous random number generation round. The target random coefficient corresponding to the initial random number generation round is 1. The target random number coefficients corresponding to each random number generation round are used to construct the difference between the target products of the current random number generation round and the previous random number round to be an integer multiple of the random prime number seed, so as to ensure that the target remainders obtained in the current random number generation round and the previous random number round are different. The target random coefficient corresponding to the current random number generation round can be determined by incrementing the target random coefficient corresponding to the previous random number generation round by 1. For example, the target random coefficients corresponding to each random number generation round can be 1, 2, 3,... in sequence. In addition, when the number of random number generation rounds is less than the dividend, the target random coefficients corresponding to each random number generation round can also be any integer less than or equal to the dividend. For example, assuming that the dividend is 10 and the number of random number generation rounds is 3, the target random coefficients corresponding to the 3 random number generation rounds can be 3, 5, and 8 respectively.
[0056] Step 103: When the number of random number generation rounds is reached, determine the target random number based on the target remainders corresponding to each random number generation round.
[0057] In the embodiment of the present invention, when the number of random number generation rounds is reached, the target random number can be determined based on the target remainders obtained in each random number generation round. When the lower limit of the random number value is 0, the target remainders obtained in each random number generation round can be directly determined as the target random number; when the lower limit of the random number value is not 0, the target random numbers are determined based on the target remainders obtained in each random number generation round and the lower limit of the random number value.
[0058] Optionally, when the lower limit of the random number value in the target random range is not 0, step 203 may include the following steps:
[0059] Step 501: For the target remainder corresponding to any random number generation round, add the value of the random number generation round corresponding to the target remainder and the lower limit of the random number value to obtain the target random number.
[0060] In the embodiment of the present invention, since the dividend is determined based on the number of values included in the lower limit and the upper limit of the random number value, when the lower limit of the random number value in the target random range is not 0, it is equivalent to first replacing the target random range with a random number generation range with 0 as the lower limit of the random number value for random number generation. If it is ensured that the target random number is within the original target random range after obtaining the target remainder, then on the basis of the target remainder, the lower limit of the random number value needs to be added, so that the target random numbers are all within the target random range.
[0061] Exemplarily, assume that the target random range is 2 - 11, then the dividend is 10. If the randomly generated random prime number seed is 7 and the number of random number generation rounds is 3, then 7 % 10 = 7; 14 % 10 = 4; 21 % 10 = 1. It can be seen that the target remainders are 7, 4, and 1 respectively. Since 1 does not belong to the target random range, the lower limit of the random value, i.e., 2, is added to the target remainder to obtain the target random numbers 9, 6, and 3, all of which are within the target random range.
[0062] In summary, in the embodiment of the present invention, in response to a random number generation instruction, based on the random number generation instruction, the number of random number generation rounds, the dividend, and the random prime number seed are determined; for any random number generation round, based on the target product and the dividend, the target remainder corresponding to the random number generation round is determined; the target product is obtained based on the random prime number seed and the target random coefficient corresponding to the random number generation round, and the target random coefficients corresponding to different random number generation rounds are different; when the number of random number generation rounds is reached, based on the target remainders corresponding to multiple random number generation rounds, multiple target random numbers are determined. In this way, by performing a remainder calculation based on the target product of the random prime number seed and the target random coefficient corresponding to the current random number generation round and the dividend in each round of random number generation, and then determining the target random number based on the obtained target remainder, it can be ensured that the target remainders obtained in different random number generation rounds are different from each other, and further ensure that multiple target random numbers are different from each other. Since the embodiment of the present invention can directly obtain different target random numbers through multiple rounds of random number generation rounds, without having to compare with historical random numbers one by one to determine whether the random numbers are repeated, it reduces the time required for the random number generation process and improves the random number generation efficiency. At the same time, there is no need to set up a list to store historical random numbers to ensure randomness. The embodiment of the present invention saves storage space while ensuring the randomness of the random numbers.
[0063] Optionally, step 102 may include the following steps:
[0064] Step 601, when the random number generation round is the initial random number generation round, calculate the target product of the random prime number seed and the target random coefficient corresponding to the random number generation round as the target divisor.
[0065] In the embodiment of the present invention, when the random number generation round is the initial random number generation round, that is, when starting to generate random numbers, the target product of the random prime number seed and the target random coefficient corresponding to the current random number generation round is determined as the target divisor.
[0066] In a possible implementation, when the number of random number generation rounds is equal to the dividend, that is, when the number of random numbers to be generated is equal to the number of values included in the target random range, the target random coefficient corresponding to the initial random number generation round can be 1. Correspondingly, the target random coefficient corresponding to the next random number generation round can be determined by incrementing the target random coefficient corresponding to the previous random number generation round by 1 unit.
[0067] In another possible implementation, when the number of random number generation rounds is less than the dividend, that is, when the number of random numbers to be generated is less than the number of values included in the target random range, the target random coefficient corresponding to the initial random number generation round can be any integer less than or equal to the dividend. Correspondingly, the target random coefficient corresponding to the next random number generation round can be any integer less than or equal to the dividend other than the target random coefficients corresponding to the previous random number generation rounds.
[0068] Step 602: Determine the target remainder corresponding to the random number generation round based on the remainder of the target divisor and the dividend.
[0069] In the embodiments of the present invention, the target remainder corresponding to the current random number generation round is determined based on the remainder of the target divisor and the dividend. The remainder of the target divisor and the dividend is calculated to obtain the target remainder corresponding to the current random number generation round.
[0070] Step 603: Determine the target random coefficient corresponding to the next random number generation round based on the target random coefficient corresponding to the random number generation round.
[0071] In the embodiments of the present invention, based on the target random coefficient corresponding to the current random number generation round, the target random coefficient corresponding to the next random number generation round is determined. When the number of random number generation rounds is equal to the dividend, that is, when the number of random numbers to be generated is equal to the number of values included in the target random range, the target random coefficient corresponding to the next random number generation round can be determined by incrementing the target random coefficient corresponding to the previous random number generation round by 1. Exemplarily, when the number of random number generation rounds is 3 and the target random range is 0-2, the dividend is 3, and the number of random number generation rounds is equal to the dividend, the target random coefficients corresponding to each random number generation round can be: 1, 2, 3. When the number of random number generation rounds is less than the dividend, that is, when the number of random numbers to be generated is less than the number of values included in the target random range, the target random coefficient corresponding to the next random number generation round can be any integer less than or equal to the dividend except the target random coefficients corresponding to the previous random number generation rounds. Exemplarily, when the number of random number generation rounds is 3 and the target random range is 0-5, the dividend is 6, and the number of random number generation rounds is less than the dividend, the target random coefficients corresponding to each random number generation round can be: 3, 5, 1.
[0072] Step 604: Based on the target random coefficient corresponding to the next random number generation round, re-execute the step of calculating the target product of the random prime number seed and the target random coefficient corresponding to the random number generation round as the target divisor until the number of random number generation rounds is reached.
[0073] In the embodiments of the present invention, based on the target random coefficient corresponding to the next random number generation round, step 501 is re-executed to sequentially obtain the target remainders corresponding to each random number generation round until the random number generation round matches the number of random number generation rounds, that is, the number of random number generation rounds is reached. Correspondingly, the target remainders in the quantity matching the number of random number generation rounds are obtained.
[0074] In the embodiments of the present invention, by determining the target random coefficients corresponding to different random number generation rounds and obtaining the target remainders in the way of taking the remainder calculation, the difference and disorder of each target remainder within the target random range are ensured, and further the randomness of the target random number within the target random range is ensured.
[0075] Exemplarily, assume that the number of random numbers generated is 10, and the target random range is 0 - 9. Then the number of rounds of random number generation is 10, and the dividend is 10. A random prime number seed of 7 is randomly generated. Since the number of rounds of random number generation is equal to the dividend, the target random coefficient corresponding to the initial round of random number generation is 1, and the target remainder corresponding to the initial round of random number generation is: 7 % 10 = 7. The target random coefficient corresponding to the next round of random number generation is 1 + 1 = 2, and the target remainder corresponding to the next round of random number generation is 2 * 7 % 10 = 4. The target random coefficient corresponding to the next round of random number generation is determined in turn, and the target remainder is calculated until the random number generation round is carried out 10 times. The calculation process of the target remainder corresponding to the third round of random number generation to the last round of random number generation can be: 3 * 7 % 10 = 1; 4 * 7 % 10 = 8; 5 * 7 % 10 = 5; 6 * 7 % 10 = 2; 7 * 7 % 10 = 9; 8 * 7 % 10 = 6; 9 * 7 % 10 = 3; 10 * 7 % 10 = 0. It can be obtained that the target remainders corresponding to each round of random number generation are: 7, 4, 1, 8, 5, 2, 9, 6, 3, 0. When the lower limit of the random number in the target range is 0, the target remainders can be directly determined as the target random numbers, that is, 10 random numbers that are distributed in the target random range of 0 - 10 and are different from each other are output in turn.
[0076] Furthermore, an example is given for the scenario where the lower limit of the random value in the target range is not 0: Assume the number of random numbers generated is 10, and the target random range is 1 - 10. Then the number of rounds of random number generation is 10, and the dividend is 10. A random prime number seed of 7 is randomly generated. Since the number of rounds of random number generation is equal to the dividend, the target random coefficient corresponding to the initial round of random number generation is 1, and the target remainder corresponding to the initial round of random number generation is: 7 % 10 = 7. The target random coefficient corresponding to the next round of random number generation is 1 + 1 = 2, and the target remainder corresponding to the next round of random number generation is 2 * 7 % 10 = 4. The target random coefficient corresponding to the next round of random number generation is determined in sequence, and the target remainder is calculated until the random number generation round is carried out 10 times. The calculation process of the target remainder corresponding to the third round of random number generation to the last round of random number generation can be: 3 * 7 % 10 = 1; 4 * 7 % 10 = 8; 5 * 7 % 10 = 5; 6 * 7 % 10 = 2; 7 * 7 % 10 = 9; 8 * 7 % 10 = 6; 9 * 7 % 10 = 3; 10 * 7 % 10 = 0. It can be obtained that the target remainders corresponding to each round of random number generation are: 7, 4, 1, 8, 5, 2, 9, 6, 3, 0. In the case where the lower limit of the random value in the target range is not 0, it is necessary to add the lower limit of the random value to the basis of each target remainder to obtain the target random numbers as 8, 5, 2, 9, 6, 3, 10, 7, 4, 1, that is, 10 random numbers that are distributed in the target random range of 1 - 9 and are different from each other are output in sequence.
[0077] Further, a scenario where the number of generated random numbers is less than the number of values included in the target random range is used as an example: Assume that the number of generated random numbers is 3, and the target random range is 2 - 11. Then the number of random number generation rounds is 3, and the dividend is 10. A randomly generated random prime number seed is 7. Since the number of generated random numbers is less than the number of values included in the target random range, that is, the number of random number generation rounds is less than the dividend, the target random coefficient corresponding to the initial random number generation round can be any integer less than or equal to the dividend, such as 3. Then the target remainder corresponding to the initial random number generation round is: 3 * 7 % 10 = 1. The target random coefficient corresponding to the second random number generation round is any integer less than or equal to the dividend other than the target random coefficient corresponding to the previous random number generation round, such as 5. The third random number generation round is 7. Then the target remainders corresponding to the second random number generation round and the third random number generation round can be: 5 * 7 % 10 = 5; 7 * 7 % 10 = 9. It can be obtained that the target remainders corresponding to each random number generation round are: 1, 5, 9. When the lower limit of the random value in the target range is not 0, it is necessary to add the lower limit of the random value to each target remainder to obtain the target random numbers as 3, 7, 11, that is, 3 non - identical random numbers distributed between the target random range 2 - 11 are output in sequence.
[0078] Optionally, when the number of random prime number seeds is two, the two random prime number seeds include the first random prime number seed and the second random prime number seed. Step 602 may include the following steps:
[0079] Step 701, calculate the remainder of the target divisor divided by the dividend to obtain the first remainder.
[0080] Step 702, determine the first divisor as the product of the first remainder and the first random prime number seed; the first random prime number is the random prime number seed other than the second random prime number seed among the two random prime number seeds, and the second random prime number seed is the random prime number seed used when determining the target divisor.
[0081] Step 703, determine the target remainder corresponding to the random number generation round based on the remainder of the first divisor divided by the dividend.
[0082] In the embodiments of the present invention, the selected random prime number seed will affect the apparent randomness of the result. To obtain a better random effect, multiple iterations can be performed in each random number generation round. Each time, a different random prime number seed is selected for iterative calculation to determine the target remainder.
[0083] When there are two random prime number seeds, calculate the target product of the target random coefficient corresponding to the second random prime number seed and the current random number generation round, that is, the remainder of the target divisor divided by the dividend, to obtain the first remainder. Iterate again based on the first remainder, that is, use the product of the first remainder and the first random prime number seed as the first divisor. Calculate the remainder of the first divisor divided by the dividend to obtain the target remainder corresponding to the current random number generation round. Among them, the first random prime number is the random prime number seed other than the second random prime number seed among the two random prime number seeds, and the second random prime number seed is the random prime number seed used to determine the target divisor.
[0084] It can be understood that when there are at least two random prime number seeds, for each round of random number generation, multiple iterations can be performed based on at least two random prime number seeds. For example, the number of iterations can be 8 times to ensure the randomness of the random numbers. Specifically, for each round of random number generation, the generation process of the target remainder can be as follows:
[0085] A) Randomly generate k random prime number seeds p1...pk, and the starting target random coefficient i = 1. The number of random numbers to be generated is N, and the target random range is from 0 to N - 1, that is, the dividend is N;
[0086] B) Take the first random prime number seed p1 and calculate the target remainder y1: y1 = p1 * i % N
[0087] C) Take the second random prime number seed p2 and calculate the remainder y2: y2 = p2 * y1 % N
[0088] D) Take the third random prime number seed p3 and calculate the target remainder y3: y3 = p3 * y2 % N ......
[0089] E) Sequentially take the last random prime number seed pk and calculate the target remainder xk, which is the i-th target remainder output in the current random number generation round. And since the number of random numbers to be generated is equal to the dividend, it is equivalent that xk is the i-th non-repeating target random number output.
[0090] For the next round of random number generation, let i change from 1 to N, and each time return to step B to recalculate, then N target random numbers can be obtained. These random numbers are distributed between 0 and N - 1 and are all different.
[0091] Exemplarily, taking the example of randomly generating 15 random numbers within the target random range of 0 - 14, first randomly generate 3 random prime number seeds between 7.5 - 30, which are: 11, 17, 23. Let the target random coefficient i vary from 1 to 15, and the dividend is 15. Then the calculation results of each round of random number generation iterations based on the 3 random prime number seeds can be as follows (one line represents the iterative process of one round of random number generation):
[0092] 1 * 11 % 15 = 11, 11 * 17 % 15 = 7, 7 * 23 % 15 = 11;
[0093] 2 * 11 % 15 = 7, 7 * 17 % 15 = 14, 14 * 23 % 15 = 7;
[0094] 3 * 11 % 15 = 3, 3 * 17 % 15 = 6, 6 * 23 % 15 = 3;
[0095] 4 * 11 % 15 = 14, 14 * 17 % 15 = 13, 13 * 23 % 15 = 14;
[0096] 5 * 11 % 15 = 10, 10 * 17 % 15 = 5, 5 * 23 % 15 = 10;
[0097] 6 * 11 % 15 = 6, 6 * 17 % 15 = 12, 12 * 23 % 15 = 6;
[0098] 7 * 11 % 15 = 2, 2 * 17 % 15 = 4, 4 * 23 % 15 = 2;
[0099] 8 * 11 % 15 = 13, 13 * 17 % 15 = 11, 11 * 23 % 15 = 13;
[0100] 9 * 11 % 15 = 9, 9 * 17 % 15 = 3, 3 * 23 % 15 = 9;
[0101] 10 * 11 % 15 = 5, 5 * 17 % 15 = 10, 10 * 23 % 15 = 5;
[0102] 11 * 11 % 15 = 1, 1 * 17 % 15 = 2, 2 * 23 % 15 = 1;
[0103] 12 * 11 % 15 = 12, 12 * 17 % 15 = 9, 9 * 23 % 15 = 12;
[0104] 13 * 11 % 15 = 8, 8 * 17 % 15 = 1, 1 * 23 % 15 = 8;
[0105] 14 * 11 % 15 = 4, 4 * 17 % 15 = 8, 8 * 23 % 15 = 4;
[0106] 15 * 11 % 15 = 0, 0 * 17 % 15 = 0, 0 * 23 % 15 = 0.
[0107] It can be obtained that the target remainders output in each round are: 11, 7, 3, 14, 10, 6, 2, 13, 9, 5, 1, 12, 8, 4, 0. Correspondingly, since the lower limit of the random value is 0, the target remainder can be directly determined as the target random number, that is, 15 target random numbers 11, 7, 3, 14, 10, 6, 2, 13, 9, 5, 1, 12, 8, 4, 0 randomly distributed between 0 and 14 are obtained.
[0108] In the embodiment of the present invention, by performing multiple iterations in each round of random number generation rounds, the quality of the random number is improved, making the random number closer to white noise.
[0109] In a possible implementation manner, the random number generation process of the embodiment of the present invention can be implemented based on a hardware circuit in an IP (Intellectual Property) in a chip, that is, an integrated circuit module with intellectual property rights, which can also be called an IP core (IP Core). Specifically, it can be implemented using a multiplier circuit, a divider circuit, and a remainder-taking circuit, and the multiplier circuit, the divider circuit, and the remainder-taking circuit are used as 1 converter unit. When the random prime number seed is 1 in a random number generation round, that is, when only one iteration is required, the random number generator can include 1 converter unit. When the random prime number seed is at least two in a random number generation round, that is, when multiple iterations are required, the random number generator can include multiple converter units, and the multiple converter units are connected in series to perform multiple iterations based on at least two random prime number seeds. Each converter unit can perform a remainder-taking calculation using one random prime number seed and transfer the result of the current remainder-taking calculation to the next converter unit until the iteration ends, and the remainder calculated by the last converter unit is determined as the target remainder corresponding to the current random number generation round, completing the random number generation process of the current random number generation round. In this way, non-repeating random number generation can be achieved with a relatively low hardware circuit cost, reducing the tape-out cost. At the same time, the number of iterations can be adjusted by adjusting the number of converter units to obtain different random number generation effects.
[0110] Referring to Figure 2 , a block diagram of a random number generation device provided by an embodiment of the present invention is shown. As Figure 2 shown, the device may specifically include:
[0111] A first determination module 801, configured to determine the random number generation round number, the dividend, and the random prime number seed based on the random number generation instruction in response to the random number generation instruction;
[0112] A second determination module 802, configured to, for any random number generation round, determine a target remainder corresponding to the random number generation round based on a target product and the dividend; the target product is obtained based on the random prime number seed and a target random coefficient corresponding to the random number generation round, and the target random coefficients corresponding to different random number generation rounds are different;
[0113] A third determination module 803, configured to, when the random number generation round number is reached, determine a plurality of target random numbers based on the target remainders corresponding to a plurality of the random number generation rounds.
[0114] An embodiment of the present invention provides a random number generation device, which, in response to a random number generation instruction, determines a random number generation round number, a dividend, and a random prime number seed based on the random number generation instruction; for any random number generation round, determines a target remainder corresponding to the random number generation round based on a target product and the dividend; the target product is obtained based on the random prime number seed and a target random coefficient corresponding to the random number generation round, and the target random coefficients corresponding to different random number generation rounds are different; when the random number generation round number is reached, determines a plurality of target random numbers based on the target remainders corresponding to a plurality of the random number generation rounds. In this way, by performing a remainder calculation based on the target product of the random prime number seed and the target random coefficient corresponding to the current random number generation round and the dividend in each random number generation round, and then determining the target random number based on the obtained target remainder, it can be ensured that the target remainders obtained in different random number generation rounds are different from each other, and further ensure that the plurality of target random numbers are different from each other. Since the embodiment of the present invention can directly obtain different target random numbers through multiple random number generation rounds, there is no need to compare with historical random numbers one by one to determine whether the random numbers are repeated, which reduces the time required for the random number generation process and improves the random number generation efficiency. At the same time, there is no need to set up a list to store historical random numbers to ensure randomness. The embodiment of the present invention saves storage space while ensuring the randomness of the random numbers.
[0115] Optionally, the device further includes:
[0116] A fourth determination module, configured to determine the random number generation round number and the dividend based on the random number generation quantity and the target random range in the random number generation requirement;
[0117] A first generation module, configured to randomly generate the random prime number seed;
[0118] A second generation module, configured to generate the random number generation instruction based on the random number generation round number, the dividend, and the random prime number seed.
[0119] Optionally, the fourth determination module includes:
[0120] A first parsing module, configured to parse the random number generation requirement to obtain the random number generation quantity and the target random range; the random number generation quantity is less than or equal to the target quantity of the numerical values included in the target random range.
[0121] A first determination sub-module, configured to determine the random number generation quantity as the random number generation rounds.
[0122] A second determination sub-module, configured to determine the dividend based on the target quantity.
[0123] Optionally, the first generation module includes:
[0124] A third determination sub-module, configured to determine half of the dividend as the lower limit of random seed generation and twice the dividend as the upper limit of random seed generation.
[0125] A first selection module, configured to randomly select a prime number between the upper limit of random seed generation and the lower limit of random seed generation as the random prime seed.
[0126] Optionally, the second determination module 802 includes:
[0127] A fourth determination sub-module, configured to calculate the target product of the random prime seed and the target random coefficient corresponding to the random number generation rounds as the target divisor when the random number generation rounds are the initial random number generation rounds.
[0128] A fifth determination sub-module, configured to determine the target remainder corresponding to the random number generation rounds based on the remainder of the target divisor and the dividend.
[0129] A sixth determination sub-module, configured to determine the target random coefficient corresponding to the next random number generation rounds based on the target random coefficient corresponding to the random number generation rounds.
[0130] A first execution module, configured to re-execute the calculation of the target product of the random prime seed and the target random coefficient corresponding to the random number generation rounds as the target divisor based on the target random coefficient corresponding to the next random number generation rounds until the random number generation quantity is reached.
[0131] Optionally, when the lower limit of the random value in the target random range is not 0, the third determination module 803 includes:
[0132] A first calculation module, configured to add the target remainder corresponding to any random number generation rounds to the value of the lower limit of the random value to obtain the target random number.
[0133] Optionally, when the number of the random prime number seeds is two, the fifth determination sub-module includes:
[0134] A second calculation module, configured to calculate a remainder of the target divisor divided by the dividend to obtain a first remainder;
[0135] A seventh determination sub-module, configured to determine a product of the first remainder and a first random prime number seed as a first divisor; the first random prime number is a random prime number seed other than a second random prime number seed among the two random prime number seeds, and the second random prime number seed is a random prime number seed used when determining the target divisor;
[0136] An eighth determination sub-module, configured to determine a target remainder corresponding to the random number generation round based on a remainder of the first divisor divided by the dividend.
[0137] Refer to Figure 3 , which is a schematic structural diagram of an electronic device provided in an embodiment of the present invention. As Figure 3 shown, the electronic device includes: a processor, a memory, a communication interface, and a communication bus. The processor, the memory, and the communication interface complete communication with each other through the communication bus; the memory is used to store executable instructions, and the executable instructions cause the processor to execute the random number generation method in the foregoing embodiment. The executable instructions may form a program.
[0138] An embodiment of the present invention provides a readable storage medium, on which executable instructions are stored. When executed by one or more processors, the executable instructions cause the processor to execute the random number generation method in the foregoing embodiment.
[0139] Each embodiment in this specification is described in a progressive manner. Each embodiment focuses on the differences from other embodiments. The same and similar parts among the embodiments may be referred to each other.
[0140] Those skilled in the art should understand that the embodiments of the present invention may be provided as a method, apparatus, or computer program product. Therefore, the embodiments of the present invention may take the form of an all-hardware embodiment, an all-software embodiment, or an embodiment combining software and hardware aspects. Moreover, the embodiments of the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk memory, CD-ROM, optical memory, etc.) that contain computer-usable program code. It should be noted that all actions of obtaining signals, information, or data in this application are carried out on the premise of complying with the corresponding data protection regulations and policies of the country where it is located and obtaining authorization from the owner of the corresponding device. The embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of methods, terminal devices (systems), and computer program products according to the embodiments of the present invention. It should be understood that each flow and / or block in the flowchart and / or block diagram, as well as the combination of flows and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing terminal devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal devices generate a device for implementing the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 a block or multiple blocks.
[0141] These computer program instructions can also be stored in a computer-readable memory that can guide a computer or other programmable data processing terminal device to work in a predictive manner, so that the instructions stored in the computer-readable memory generate a manufactured product including an instruction device, and the instruction device implements the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 a block or multiple blocks.
[0142] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device, so that a series of operation steps are executed on the computer or other programmable terminal device to generate a computer-implemented process. Therefore, the instructions executed on the computer or other programmable terminal device provide steps for implementing the functions specified in one process Figure 1 one process or multiple processes and / or blocks Figure 1 a block or multiple blocks.
[0143] Although the preferred embodiments of the embodiments of the present invention have been described, those skilled in the art can make additional changes and modifications to these embodiments once they know the basic creative concepts. Therefore, the appended claims are intended to be interpreted to include the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.
[0144] Finally, it should also be noted that in this text, relational terms such as first and second are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the term "comprising", "including" or any other variant thereof is intended to cover non-exclusive inclusion, so that a process, method, article or terminal device comprising a series of elements not only includes those elements, but also includes other elements not expressly listed, or further includes elements inherent to such process, method, article or terminal device. Without further limitation, an element defined by the statement "comprising an..." does not exclude the existence of additional identical elements in the process, method, article or terminal device comprising the said element.
[0145] The above has introduced in detail a method for generating an instruction stream file, an apparatus for generating an instruction stream file, an electronic device and a readable storage medium provided by the present invention. Specific examples are used in this text to elaborate on the principle and implementation manner of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea; at the same time, for those of ordinary skill in the art, according to the idea of the present invention, there will be changes in the specific implementation manner and application scope. In summary, the content of this specification should not be construed as a limitation to the present invention.
Claims
1. A method for generating a random number, characterized in that: The method comprises: The random number generation requirement is parsed to obtain the random number generation quantity and the target random range; the random number generation quantity is less than or equal to the target number of values included in the target random range; Determine the number of random number generation as the number of random number generation rounds; Based on the target amount, determining a divisor; Randomly generate a random prime number seed; Generate a random number generation instruction based on the random number generation round number, the divisor and the random prime number seed; In response to the random number generation instruction, for any random number generation round, based on the target product of the random prime number seed and the target random coefficient corresponding to the random number generation round, a target dividend is determined, and a remainder calculation is performed on the target dividend and the divisor to determine a target remainder corresponding to the random number generation round; different random number generation rounds correspond to different target random coefficients, and the target random coefficient is an integer less than or equal to the divisor; When the number of random number generation rounds is reached, a plurality of target random numbers are determined based on target remainders corresponding to a plurality of the random number generation rounds.
2. The method according to claim 1, characterized in that The randomly generating a random prime number seed comprises: Determine half of the divisor as a lower limit for random seed generation, and determine twice of the divisor as an upper limit for random seed generation; A prime number is randomly selected from the random seed generation upper limit and the random seed generation lower limit as the random prime number seed.
3. The method according to claim 1, characterized in that The method of determining a target dividend based on the target product of the random prime number seed and the target random coefficient corresponding to the random number generation round, and performing remainder calculation on the target dividend and the divisor to determine the target remainder corresponding to the random number generation round includes: When the random number generation round is an initial random number generation round, calculating a target product of the random prime number seed and a target random coefficient corresponding to the random number generation round as a target dividend; Performing a remainder calculation on the target dividend and the divisor to determine a target remainder corresponding to the random number generation round; Determining a target random coefficient corresponding to a next random number generation round based on the target random coefficient corresponding to the random number generation round; Based on the target random coefficient corresponding to the next random number generation round, the target product of the random prime number seed and the target random coefficient corresponding to the random number generation round is calculated and re-executed as the target dividend until the number of random number generation rounds is reached.
4. The method according to claim 1, characterized in that: When the lower limit of the random value in the target random range is not 0, the determining of multiple target random numbers based on target remainders corresponding to multiple random number generation rounds includes: For any target remainder corresponding to the random number generation round, the target remainder corresponding to the random number generation round is added to the value of the lower limit of the random value to obtain the target random number.
5. The method according to claim 3, characterized in that: When the number of the random prime number seeds is two, performing a remainder calculation on the target dividend and the divisor to determine a target remainder corresponding to the random number generation round includes: Calculate the remainder of the target dividend and the divisor to obtain a first remainder; Determine the product of the first remainder and the first random prime number seed as the first dividend; the first random prime number is a random prime number seed other than the second random prime number seed among the two random prime number seeds, and the second random prime number seed is a random prime number seed used when determining the target dividend; Based on a remainder between the first dividend and the divisor, a target remainder corresponding to the random number generation round is determined.
6. A random number generating device, characterized in that: The device comprises: A module for parsing random number generation requirements to obtain the number of random number generation and a target random range; the number of random number generation is less than or equal to the target number of values included in the target random range; A module for determining the number of random number generation as the number of random number generation rounds; module for determining a divisor based on the target quantity; Module for randomly generating random prime number seeds; A module for generating random number generation instructions based on the random number generation round number, the divisor and the random prime seed; A module for determining, in response to the random number generation instruction, a target dividend based on a target product of the random prime number seed and the target random coefficient corresponding to the random number generation round for any random number generation round, and performing a remainder calculation on the target dividend and the divisor to determine a target remainder corresponding to the random number generation round; different random number generation rounds correspond to different target random coefficients, and the target random coefficient is an integer less than or equal to the divisor; A module for determining a plurality of target random numbers based on target remainders corresponding to a plurality of the random number generation rounds when the number of random number generation rounds is reached.
7. An electronic device, characterized in that: include: A processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other via the communication bus; The memory is used to store executable instructions, and the executable instructions enable the processor to execute the random number generation method according to any one of claims 1 to 5.
8. A readable storage medium, characterized in that: Executable instructions are stored thereon, and when executed by one or more processors, the processors are caused to execute the random number generation method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Method and device for generating big prime number rapidly
CN105553659A
Software random number generation method and device
CN116243887A