Random number generation method and device, electronic equipment and storage medium
By determining the random number generation rounds, dividends and random prime seeds in the random number generator, and calculating the target remainder to generate random numbers that are different from each other, the problems of large time consumption and large storage space consumption of traditional random number generators are solved, and efficient and storage-saving random number generation is achieved.
Patent Information
- Application Number
- CN202510350521.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-24
- Publication Date
- 2025-04-22
- 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 number of random number generation rounds, dividends and random prime seeds, and calculate the target remainder based on the target product and dividend for each round of random number generation round, ensuring that the target remainders of different rounds are different, thereby generating target random numbers that are different from each other.
This method reduces the comparison operation in the random number generation process, reduces time consumption, improves generation efficiency, and saves storage space to ensure the randomness of random numbers.
Smart Images

Figure CN119861903A_ABST
Abstract
Description
Technical Field
[0001] The embodiments of the present invention relate to the field of computer technology, and in particular to a method, device, electronic device and storage medium for generating random numbers. Background Art
[0002] With the rapid development of information technology, the demand for high-quality random numbers in the fields of data processing and information security is growing. A traditional non-repeating random number generator can sequentially output multiple non-repeating random numbers within a specified interval. For example, if the random number generation interval is set to 0-100, the non-repeating random number generator will randomly output 100 values in sequence. These 100 values are different from each other and range from 0 to 100.
[0003] In the related art, the generation of random numbers is 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 a new random number R is generated, it needs to be compared with the historical random numbers in the list in turn to determine whether it is repeated with the historical random number. If the list does not contain the random number R, R is added to the list and output as a random number; if the list contains the random number R, a new random number is regenerated and compared again. This random number generation method requires that the generated random number be compared with the historical random number one by one each time to ensure the randomness and non-repetitiveness of the random number, resulting in too much time spent on repeated random number generation, and storing the historical random number in the list requires a large storage space. Summary of the invention
[0004] Embodiments of the present invention provide a method, device, electronic device and storage medium for generating a random number.
[0005] In order to solve the above problem, an embodiment of the present invention discloses a method for generating a random number, the method comprising: In response to a random number generation instruction, determining 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, based on the target product and the dividend, determine 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 different random number generation rounds correspond to different target random coefficients; 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.
[0006] On the other hand, an embodiment of the present invention discloses a random number generation device, the device comprising: A first determination module, configured to determine, in response to a random number generation instruction, a random number generation round number, a dividend, and a random prime number seed based on the random number generation instruction; A second determination module is used to determine, for any random number generation round, 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 different random number generation rounds correspond to different target random coefficients; The third determination module is used to determine multiple target random numbers based on target remainders corresponding to multiple random number generation rounds when the number of random number generation rounds is reached.
[0007] On the other hand, an embodiment of the present invention discloses an electronic device, comprising: a processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other through the communication bus; the memory is used to store executable instructions, and the executable instructions enable the processor to execute the aforementioned method.
[0008] The embodiment of the present invention further discloses a readable storage medium on which executable instructions are stored. When executed by one or more processors, the processors execute the method as described above.
[0009] The embodiment of the present invention includes the following advantages: in the random number generation method provided in the embodiment of the present invention, when receiving a random number generation instruction, the random number generation round number, the dividend and the random prime number seed are determined based on the random number generation instruction; for any random number generation round, the target remainder corresponding to the current random number generation round is determined 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 random coefficients corresponding to different random number generation rounds are different; when the random number generation round number is reached, multiple target random numbers are determined based on the target remainders corresponding to multiple random number generation rounds. In this way, by performing 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 then 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, it is not necessary to compare the random numbers with the 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 efficiency of random number generation. At the same time, there is no need to set up a list to store the historical random numbers to ensure randomness. The embodiment of the present invention saves storage space while ensuring the randomness of the random numbers. BRIEF DESCRIPTION OF THE DRAWINGS
[0010] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments of the present invention will be briefly introduced below. Obviously, the accompanying drawings in the following description are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative labor.
[0011] Figure 1 is a flowchart of a method for generating a random number provided by an embodiment of the present invention; Figure 2 is a block diagram of a random number generation device provided by an embodiment of the present invention; Figure 3 It is a structural schematic diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0012] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0013] Reference Figure 1 , shows a flow chart of the steps of a method for generating a random number provided by an embodiment of the present invention, such as Figure 1 As shown, the method may specifically include the following steps: Step 101: 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.
[0014] In an embodiment of the present invention, the random number generator responds to the random number generation instruction, parses the random number generation instruction, and determines the random number generation rounds, dividend, and 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, dividend, and random prime number seed, or may include the number of random number generation and the target random range. In this case, the random number generation rounds and dividend may be determined based on the number of random number generation and the target random range, and a random prime number seed may be randomly generated. The information contained in the random number generation instruction may be set according to actual needs.
[0015] The number of random number generation rounds is used to represent the number of random number generation rounds to be performed in the random number generation process. The random prime seed is a transformation coefficient that needs to be used in the random number generation process. The random prime seed can be any prime number generated randomly. A prime number (also known as a prime number) refers to a natural number greater than 1 that has no other factors except 1 and itself.
[0016] Optionally, the embodiment of the present invention may further include the following steps: Step 201: Determine the number of random number generation rounds and the dividend based on the random number generation quantity and the target random range in the random number generation requirement.
[0017] In an embodiment of the present invention, the processor will obtain a random number generation requirement, wherein the random number generation requirement may be obtained by receiving an external input, or may be a requirement obtained according to the current business progress and matching the current business progress. The random number generation requirement may be determined according to different application scenarios, such as random transmission of data communication, that is, the transmitting end sends a group of random and disordered data to the receiving end, and the receiving end restores it to the original sequence, which has the effect of encryption; random traversal RAM self-check, that is, the entire RAM interval is divided into multiple self-checks, and non-repetitive random addresses are generated periodically and sequentially, and only the content of the address is checked. Since the addresses are not repeated, the self-check of all areas will eventually be completed.
[0018] The random number generation requirements may include the number of random numbers to be generated, the target random range, and the data type (such as integers, floating point numbers). According to the number of random numbers to be generated and the target random range in the random number generation requirements, the number of random number generation rounds and the dividend are determined. Among them, the target random range is used to define the limited range of random numbers in the random number generation process, that is, only one value can be randomly generated in the target random range during the random number generation process. The target random range includes the upper limit 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.
[0019] The number of random number generation rounds is determined based on the number of random number generation, and the dividend is determined based on the target random range.
[0020] Optionally, step 201 may include the following steps: Step 301: 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 number of values included in the target random range.
[0021] In an embodiment of the present invention, the random number generation requirement is parsed to obtain the number of random number generation and the target random range. In order to ensure the non-repetitiveness of random numbers, the number of random number generation is less than or equal to the target number of values contained in the target random range. The target random range can be composed of a random value upper limit and a random value lower limit. The target number of values contained in the target random range can be the number of integers contained between the random value upper limit and the random value lower limit. Exemplarily, if the target random range is 0-3, there are 4 values contained between 0-3, which are 0, 1, 2, and 3, respectively, that is, the target number is 4, then the number of random number generation is less than or equal to 4.
[0022] Exemplarily, the random number generation quantity in the random number generation requirement can be parsed. If the quantity is directly specified in the random number generation requirement, the value is recorded as the random number generation quantity. If the quantity depends on certain conditions or parameters (such as system status), the random number generation quantity is dynamically calculated based on these conditions and parameters. The random number value range in the random number generation requirement is usually parsed, including a random number upper limit and a random number lower limit, which defines the closed interval of the random number generation range.
[0023] Step 302: Determine the number of random number generation as the number of random number generation rounds.
[0024] In the embodiment of the present invention, each random number generation round is used to generate a target random number. Therefore, the number of random number generation is equal to the number of random number generation rounds, that is, the number of random number generation rounds is equal to the number of random number generation rounds required to generate the required number of random numbers.
[0025] Step 303: Determine the dividend based on the target number.
[0026] In the embodiment of the present invention, the target number of values included between the upper limit of the random value and the lower limit of the random value can be determined as the dividend. For example, if the target random range is 0-3, the dividend can be 4, and if the target random range is 2-6, the dividend can be 5.
[0027] In the embodiment of the present invention, the dividend and the number of random number generation rounds are flexibly determined according to the random number generation quantity and the target random range in the random number generation requirement, thereby improving the efficiency of determining the number of random number generation rounds and the dividend while meeting the random number generation requirement.
[0028] Step 202: randomly generate the random prime number seed.
[0029] In an embodiment of the present invention, a prime number list may be pre-set, and the prime number list may include multiple prime numbers. Exemplarily, the Sieve of Eratosthenes may be used to find prime numbers in natural numbers, and a prime number list is formed based on these prime numbers. A prime number is randomly selected from the prime number list as a random prime number seed.
[0030] Optionally, step 202 may include the following steps: Step 401: Determine half of the dividend as the lower limit of random seed generation, and determine twice of the dividend as the upper limit of random seed generation.
[0031] In the embodiment of the present invention, the selected random prime number seed will affect the apparent randomness of the result. In order to obtain a better random effect, the magnitude of the selected random prime number seed is set to be close to the dividend, for example, it can be between N / 2 and N*2 (N is the dividend).
[0032] The half of the dividend is determined as the lower limit of random seed generation, and the twice of the dividend is determined as the upper limit of random seed generation. The closed interval between the lower limit of random seed generation and the upper limit of random seed generation is used as the random selection range of random prime number seed.
[0033] Step 402: Randomly select a prime number from the random seed generation upper limit and the random seed generation lower limit as the random prime number seed.
[0034] In the embodiment of the present invention, a prime number is randomly selected from the upper limit of random seed generation and the lower limit of random seed generation as the random prime number seed. Specifically, a first prime number list corresponding to the selection range defined by the upper limit of random seed generation and the lower limit of random seed generation can be determined, and a prime number is randomly selected from the first prime number list as the random prime number seed.
[0035] In the embodiment of the present invention, half of the dividend is determined as the lower limit of random seed generation, and twice the dividend is determined as the upper limit of random seed generation, which can improve the random effect of the target random number and ensure the randomness of the target random number.
[0036] Step 203: Generate the random number generation instruction based on the random number generation round number, the dividend and the random prime number seed.
[0037] In an embodiment of the present invention, a random number generation instruction is generated based on the number of random number generation rounds, the dividend, and the random prime number seed. The random number generation instruction may be an instruction generated internally by the random number generator, or an instruction received from the outside. Exemplarily, the processor may 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 number of random number generation rounds, the dividend, and the random prime number seed, and then generate a random number based on the number of random number generation rounds, the dividend, and the random prime number seed. The random number generation instruction may 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 a random number based on the number of random number generation rounds, the dividend, and the random prime number seed. Exemplarily, a random number generation function in a programming language or a tool library may be used to generate a random number generation instruction based on the number of random number generation rounds, the dividend, and the random prime number seed.
[0038] In the embodiment of the present invention, random number generation instructions are generated based on random number generation requirements, which can ensure that the generation of random numbers meets the requirements of specific application scenarios, thereby improving the accuracy and effectiveness of random numbers in different application scenarios.
[0039] 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 different random number generation rounds correspond to different target random coefficients.
[0040] In the embodiment of the present invention, according to the congruence theorem "Given a positive integer m, if two integers a and b satisfy that ab is divisible by m, that is, (ab) / m obtains an integer, then the integers a and b are said to be 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 ab is divisible by m, that is, (m|ab). Among them, if the two integers a and b are divided by the same integer m and the same remainder is obtained, they are called congruent. According to the above theorem, it can be inferred that Corollary 1: If ab is not divisible by m, then the remainders of a and b divided by m are different. Further based on Corollary 1, it can be obtained: Take any prime number p, and the specified interval 0-N, take p, 2*p, 3*p, ...N*p in turn, and their remainders when divided by N are different. Specifically, since p is a prime number, p cannot be divided by N. Therefore, 2*pp=p, 3*p-2*p=p…N*p-(N-1)*p=p (equivalent to ab) are not divisible by N (equivalent to m). Therefore, the remainders of p, 2*p, 3*p,…N*p when divided by N are different.
[0041] For example, assuming p=7, 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, and it can be seen that the remainders are different.
[0042] Based on the above inference, a remainder calculation can be performed 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 to determine the target remainder corresponding to the current random number generation round, so that the target remainders corresponding to each random number generation round are different.
[0043] Among them, the target random coefficients corresponding to different random number generation rounds are different. The target random coefficients corresponding to the random number generation rounds are determined based on the target random coefficients corresponding to the previous random number generation rounds, and the target random coefficients corresponding to the initial random number generation rounds are 1. The target random coefficients corresponding to each random number generation round are to construct the difference between the target product of the current random number generation round and the previous random number generation round as an integer multiple of the random prime number seed, thereby ensuring that the target remainders obtained by the current random number generation round and the previous random number generation round are different. The target random coefficient corresponding to the current random number generation round can be determined based on the target random coefficient corresponding to the previous random number generation round in increments of 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 three random number generation rounds can be 3, 5, and 8, respectively.
[0044] Step 103: When the number of random number generation rounds is reached, determine a target random number based on a target remainder corresponding to each of the random number generation rounds.
[0045] 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 remainder obtained in each random number generation round. When the lower limit of the random value is 0, the target remainder obtained in each random number generation round can be directly determined as the target random number; when the lower limit of the random value is not 0, each target random number is determined based on the target remainder obtained in each random number generation round and the lower limit of the random value.
[0046] Optionally, when the lower limit of the random value in the target random range is not 0, step 203 may include the following steps: Step 501: For a target remainder corresponding to any of the random number generation rounds, add the target remainder corresponding to the random number generation round and the value of the lower limit of the random value to obtain the target random number.
[0047] In the embodiment of the present invention, since the dividend is determined based on the number of values contained in the lower limit of the random value and the upper limit of the random value, when the lower limit of the random 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 value to generate random numbers. If, after obtaining the target remainder, it is ensured that the target random number is within the original target random range, it is necessary to add the lower limit of the random value to the target remainder, so that the target random number is within the target random range.
[0048] For example, assuming that the target random range is 2-11, the dividend is 10. If the randomly generated random prime 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. 1 does not belong to the target random range. Then, the lower limit of the random value, i.e. 2, is added to the target remainder, and the target random numbers are 9, 6, and 3, all of which are within the target random range.
[0049] In summary, in the embodiment of the present invention, in response to the random number generation instruction, the random number generation round, the dividend and the random prime number seed are determined based on the random number generation instruction; for any random number generation round, the target remainder corresponding to the random number generation round is determined 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; when the random number generation round is reached, multiple target random numbers are determined based on the target remainders corresponding to multiple random number generation rounds. In this way, by performing 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 then 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, it is not necessary to compare the random numbers with the 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 efficiency of random number generation. At the same time, there is no need to set up a list to store the historical random numbers to ensure randomness. The embodiment of the present invention saves storage space while ensuring the randomness of the random numbers.
[0050] Optionally, step 102 may include the following steps: Step 601: When the random number generation round is an initial random number generation round, calculate a target product of the random prime number seed and a target random coefficient corresponding to the random number generation round as a target divisor.
[0051] In an embodiment of the present invention, when the random number generation round is an initial random number generation round, that is, when random numbers are just being generated, 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.
[0052] In one 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 contained in the target random range, the target random coefficient corresponding to the initial random number generation round can be 1, and accordingly, the target random coefficient corresponding to the next round of random number generation can be determined in increments of 1 based on the target random coefficient corresponding to the previous round of random number generation.
[0053] 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 contained 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, and accordingly, 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.
[0054] Step 602: Determine a target remainder corresponding to the random number generation round based on the remainder between the target divisor and the dividend.
[0055] In the embodiment of the present invention, based on the remainder of the target divisor and the dividend, the target remainder corresponding to the current random number generation round is determined. The target divisor and the dividend are subjected to a remainder calculation to obtain the target remainder corresponding to the current random number generation round.
[0056] Step 603: Based on the target random coefficient corresponding to the random number generation round, determine the target random coefficient corresponding to the next random number generation round.
[0057] In an embodiment of the present invention, the target random coefficient corresponding to the next random number generation round is determined based on the target random coefficient corresponding to the current random number generation round. In the case where 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 numerical values contained in the target random range, the target random coefficient corresponding to the next random number generation round can be determined based on the target random coefficient corresponding to the previous random number generation round in increments of 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, then the target random coefficient corresponding to each random number generation round can be: 1, 2, 3. In the case where 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 numerical values contained 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, then the target random coefficients corresponding to each random number generation round can be: 3, 5, 1.
[0058] Step 604: Based on the target random coefficient corresponding to the next random number generation round, re-execute 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.
[0059] In the embodiment of the present invention, based on the target random coefficient corresponding to the next random number generation round, step 501 is re-executed to obtain the target remainder corresponding to each random number generation round in sequence, until the random number generation round matches the random number generation round number, that is, the random number generation round number is reached, and accordingly, the target remainder matching the random number generation round number is obtained.
[0060] In the embodiment of the present invention, by determining the target random coefficient corresponding to different random number generation rounds, the target remainder is obtained by modulo calculation, thereby ensuring the difference and disorder of each target remainder within the target random range, thereby ensuring the randomness of the target random number within the target random range.
[0061] For example, assuming that the number of random number generation is 10 and the target random range is 0-9, the number of random number generation rounds is 10 and the dividend is 10. A random prime number seed is randomly generated as 7. Since the number of random number generation rounds is equal to the dividend, the target random coefficient corresponding to the initial random number generation round is 1, and the target remainder corresponding to the initial random number generation round is: 7%10=7. The target random coefficient corresponding to the next random number generation round is 1+1=2, and the target remainder corresponding to the next random number generation round 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 performed 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 random number generation round 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, each target remainder can be directly determined as the target random number, that is, 10 random numbers distributed between the target random range of 0-10 and different from each other are output in sequence.
[0062] Furthermore, an example is given for a scenario where the lower limit of the random value of the target range is not 0: Assuming that the number of random number generation is 10 and the target random range is 1-10, the number of random number generation rounds is 10 and the dividend is 10. A random prime number seed is randomly generated as 7. Since the number of random number generation rounds is equal to the dividend, the target random coefficient corresponding to the initial random number generation round is 1, and the target remainder corresponding to the initial random number generation round is: 7 % 10 = 7. The target random coefficient corresponding to the next random number generation round is 1+1=2, and the target remainder corresponding to the next random number generation round is 2*7 % 10 = 4. The target random coefficient corresponding to the next random number generation round is determined in sequence, and the target remainder is calculated until the random number generation rounds are performed 10 times. The calculation process of the target remainder corresponding to the third random number generation round to the last random number generation round 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 random number generation round are: 7, 4, 1, 8, 5, 2, 9, 6, 3, 0. 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, and the target random numbers are 8, 5, 2, 9, 6, 3, 10, 7, 4, 1. That is, 10 random numbers distributed in the target random range of 1-9 and different from each other are output in sequence.
[0063] Furthermore, an example is given in which the number of random number generation is less than the number of values included in the target random range: assuming that the number of random number generation is 3 and the target random range is 2-11, the number of random number generation rounds is 3 and the dividend is 10. A random prime number seed is randomly generated as 7. Since the number of random number generation 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 except the target random coefficient corresponding to the previous random number generation round, such as 5, and the third random number generation round is 7. Then the target remainders corresponding to the second and third random number generation rounds 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 of 3, 7, and 11. That is, 3 random numbers distributed in the target random range of 2-11 and different from each other are output in sequence.
[0064] Optionally, when the number of random prime number seeds is two, the two random prime number seeds include a first random prime number seed and a second random prime number seed. Step 602 may include the following steps: Step 701: Calculate the remainder between the target divisor and the dividend to obtain a first remainder.
[0065] Step 702: Determine the product of the first remainder and the first random prime number seed as the first divisor; 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 divisor.
[0066] Step 703: Determine a target remainder corresponding to the random number generation round based on a remainder between the first divisor and the dividend.
[0067] In the embodiment of the present invention, the selected random prime number seed will affect the apparent randomness of the result. In order to obtain a better random effect, multiple iterations can be performed in each round of random number generation, and a different random prime number seed is selected each time to perform iterative calculations to determine the target remainder.
[0068] In the case where there are two random prime number seeds, the target product based on the second random prime number seed and the target random coefficient corresponding to the current random number generation round is calculated, that is, the remainder of the target divisor and the dividend, to obtain the first remainder. Iteration is performed again based on the first remainder, that is, the product of the first remainder and the first random prime number seed is used as the first divisor. The remainder of the first divisor and the dividend is calculated 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 when determining the target divisor.
[0069] It is understandable that, when there are at least two random prime number seeds, multiple iterations may be performed based on at least two random prime number seeds for each round of random number generation. For example, the number of iterations may be 8 to ensure the randomness of the random numbers. Specifically, for each round of random number generation, the target remainder generation process may be as follows: A) Randomly generate k random prime number seeds p1...pk, and the starting target random coefficient i=1, the number of random numbers generated is N, and the target random range is 0 to N-1, that is, the dividend is N; B) Take the first random prime number seed p1 and calculate the target remainder y1: y1=p1*i%N C) Take the second random prime seed p2 and calculate the remainder y2: y2=p2*y1%N D) Take the third random prime seed p3 and calculate the target remainder y3: y3=p3*y2%N ...... E) Take the last random prime number seed pk in turn and calculate the target remainder xk, which is the i-th target remainder output by the current random number generation round. Since the number of random numbers generated is equal to the dividend, xk is equivalent to the i-th non-repeating target random number output.
[0070] For the next round of random number generation, let i change from 1 to N, and return to step B to recalculate each time to obtain N target random numbers. These random numbers are distributed between 0 and N-1 and are different from each other.
[0071] For example, taking the example of randomly generating 15 random numbers between the target random range of 0-14, firstly, three random prime number seeds are randomly generated between 7.5-30, namely: 11, 17, 23, and the target random coefficient i is changed from 1 to 15, and the dividend is 15. Then, the calculation results of each round of random number generation based on the iteration of the three random prime number seeds can be as follows (one line represents the iteration process of one round of random number generation): 1*11%15=11, 11*17%15=7, 7*23%15=11; 2*11%15=7, 7*17%15=14, 14*23%15=7; 3*11%15=3, 3*17%15=6, 6*23%15=3; 4*11%15=14, 14*17%15=13, 13*23%15=14; 5*11%15=10, 10*17%15=5, 5*23%15=10; 6*11%15=6, 6*17%15=12, 12*23%15=6; 7*11%15=2, 2*17%15=4, 4*23%15=2; 8*11%15=13, 13*17%15=11, 11*23%15=13; 9*11%15=9, 9*17%15=3, 3*23%15=9; 10*11%15=5, 5*17%15=10, 10*23%15=5; 11*11%15=1, 1*17%15=2, 2*23%15=1; 12*11%15=12, 12*17%15=9, 9*23%15=12; 13*11%15=8, 8*17%15=1, 1*23%15=8; 14*11%15=4, 4*17%15=8, 8*23%15=4; 15*11%15=0, 0*17%15=0, 0*23%15=0.
[0072] 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.
[0073] In the embodiment of the present invention, multiple iterations are performed in each round of random number generation, thereby improving the quality of random numbers and making the random numbers closer to white noise.
[0074] In a possible implementation, the random number generation process of the embodiment of the present invention can be implemented based on hardware circuits in the IP (Intellectual Property) in the chip, that is, an integrated circuit module with intellectual property rights, which can also be called an IP core (IPCore). Specifically, it can be implemented using a multiplier circuit, a divider circuit, and a remainder circuit, and the multiplier circuit, the divider circuit, and the remainder circuit are used as one converter unit. In a random number generation round, there is one random prime number seed, that is, only one iteration is required, and the random number generator may include one converter unit. In a random number generation round, there are at least two random prime number seeds, that is, when multiple iterations are required, the random number generator may 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 use a random prime number seed to perform a remainder calculation, and the result of this remainder calculation is passed 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, and the random number generation process of the current random number generation round is completed. In this way, non-repetitive random number generation can be achieved with a lower hardware circuit cost, reducing the cost of tape-out. 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.
[0075] Reference Figure 2 , shows a block diagram of a random number generation device provided by an embodiment of the present invention, such as Figure 2 As shown, the device may specifically include: A first determination module 801 is used to determine the number of random number generation rounds, the dividend, and the random prime number seed in response to the random number generation instruction based on the random number generation instruction; A second determination module 802 is used to determine, for any random number generation round, 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 different random number generation rounds correspond to different target random coefficients; The third determination module 803 is used to determine multiple target random numbers based on target remainders corresponding to multiple random number generation rounds when the number of random number generation rounds is reached.
[0076] The embodiment of the present invention provides a random number generation device, which responds to a random number generation instruction and determines the random number generation round, dividend and random prime number seed based on the random number generation instruction; for any random number generation round, determines the target remainder corresponding to the random number generation round based on the target product and 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; when the random number generation round is reached, multiple target random numbers are determined based on the target remainders corresponding to multiple random number generation rounds. In this way, by performing 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 then 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, it is not necessary to compare the random numbers with the 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 efficiency of random number generation. At the same time, there is no need to set up a list to store the historical random numbers to ensure randomness. The embodiment of the present invention saves storage space while ensuring the randomness of the random numbers.
[0077] Optionally, the device further comprises: A fourth determination module, configured to determine the number of random number generation rounds and the dividend based on the number of random number generation in the random number generation requirement and the target random range; A first generating module, used for randomly generating the random prime number seed; The second generation module is used to generate the random number generation instruction based on the random number generation round number, the dividend and the random prime number seed.
[0078] Optionally, the fourth determining module includes: A first parsing module is used 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 number of values included in the target random range; A first determining submodule, configured to determine the number of random number generation as the number of random number generation rounds; The second determination submodule is used to determine the dividend based on the target number.
[0079] Optionally, the first generating module includes: A third determination submodule is used to determine half of the dividend as a lower limit for generating a random seed, and to determine twice of the dividend as an upper limit for generating a random seed; The first selection module is used to randomly select a prime number from the random seed generation upper limit and the random seed generation lower limit as the random prime number seed.
[0080] Optionally, the second determining module 802 includes: a fourth determination submodule, configured to calculate, when the random number generation round is an initial random number generation round, a target product of the random prime number seed and a target random coefficient corresponding to the random number generation round as a target divisor; a fifth determination submodule, configured to determine a target remainder corresponding to the random number generation round based on the remainder between the target divisor and the dividend; A sixth determination submodule, configured to determine 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; The first execution module is used to re-execute the target product of the random prime number seed and the target random coefficient corresponding to the next random number generation round as a target divisor based on the target random coefficient corresponding to the next random number generation round, until the random number generation round number is reached.
[0081] Optionally, when the lower limit of the random value in the target random range is not 0, the third determination module 803 includes: The first calculation module is used to add the target remainder corresponding to any of the random number generation rounds to the value of the lower limit of the random value to obtain the target random number.
[0082] Optionally, when the number of the random prime number seeds is two, the fifth determining submodule includes: A second calculation module is used to calculate the remainder of the target divisor and the dividend to obtain a first remainder; a seventh determination submodule, configured to determine the 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 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 divisor; An eighth determination submodule is used to determine a target remainder corresponding to the random number generation round based on a remainder between the first divisor and the dividend.
[0083] Reference Figure 3 , is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. Figure 3As shown, the electronic device includes: a processor, a memory, a communication interface and a communication bus, wherein the processor, the memory and the communication interface communicate with each other through 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 of the above embodiment. The executable instructions can form a program.
[0084] An embodiment of the present invention provides a readable storage medium having executable instructions stored thereon. When executed by one or more processors, the processors are enabled to execute the random number generation method of the aforementioned embodiment.
[0085] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The same or similar parts between the various embodiments can be referenced to each other.
[0086] It should be understood by those skilled in the art that the embodiments of the present invention can be provided as methods, devices, or computer program products. Therefore, the embodiments of the present invention can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the embodiments of the present invention can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes. It should be noted that all actions of acquiring signals, information or data in this application are carried out under the premise of complying with the corresponding data protection laws and policies of the country of residence and with the authorization given by the owner of the corresponding device. The embodiments of the present invention are described with reference to the flowcharts and / or block diagrams of the methods, terminal devices (systems), and computer program products according to the embodiments of the present invention. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor or other programmable data processing terminal device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing terminal device generate instructions for implementing the processes in the process Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0087] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing terminal device to operate in a predictable manner, so that the instructions stored in the computer readable memory produce a manufactured product including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0088] These computer program instructions can also be loaded onto a computer or other programmable data processing terminal device so that a series of operating steps are executed on the computer or other programmable terminal device to produce a computer-implemented process, thereby providing instructions for executing on the computer or other programmable terminal device to implement the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0089] Although the preferred embodiments of the present invention have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the embodiments of the present invention.
[0090] Finally, it should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or terminal device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or terminal device including the elements.
[0091] The above is a detailed introduction to an instruction stream file generation method, an instruction stream file generation device, an electronic device and a readable storage medium provided by the present invention. Specific examples are used in this article to illustrate the principles and implementation methods 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 a person skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.
Claims
1. A method for generating a random number, characterized in that: The method comprises: In response to a random number generation instruction, determining 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, based on the target product and the dividend, determine 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 different random number generation rounds correspond to different target random coefficients; 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 method further comprises: Determine the number of random number generation rounds and the dividend based on the random number generation quantity and the target random range in the random number generation requirement; Randomly generate the random prime number seed; The random number generation instruction is generated based on the random number generation round number, the dividend, and the random prime number seed.
3. The method according to claim 2, characterized in that The step of determining the number of random number generation rounds and the dividend based on the random number generation quantity and the target random range in the random number generation requirement includes: 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 number, the dividend is determined.
4. The method according to claim 3, characterized in that The randomly generating a random prime number seed comprises: Determine half of the dividend as a lower limit for generating a random seed, and determine twice of the dividend as an upper limit for generating a random seed; 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.
5. The method according to claim 1, characterized in that The determining, based on the target product and the dividend, a 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 divisor; Determining a target remainder corresponding to the random number generation round based on the remainder between the target divisor and the dividend; 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 a target divisor until the number of random number generation rounds is reached.
6. The method according to claim 2, 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.
7. The method according to claim 5, characterized in that In a case where the number of the random prime number seeds is two, determining the target remainder corresponding to the random number generation round based on the remainder between the target divisor and the dividend includes: Calculate the remainder of the target divisor and the dividend to obtain a first remainder; Determine the product of the first remainder and the first random prime number seed as the first divisor; 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 divisor; Based on a remainder between the first divisor and the dividend, a target remainder corresponding to the random number generation round is determined.
8. A random number generating device, characterized in that: The device comprises: A first determination module, configured to determine, in response to a random number generation instruction, a random number generation round number, a dividend, and a random prime number seed based on the random number generation instruction; A second determination module is used to determine, for any random number generation round, 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 different random number generation rounds correspond to different target random coefficients; The third determination module is used to determine multiple target random numbers based on target remainders corresponding to multiple random number generation rounds when the number of random number generation rounds is reached.
9. 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 7.
10. 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 7.
Citation Information
Patent Citations
Method and device for generating big prime number rapidly
CN105553659A
Software random number generation method and device
CN116243887A
Random number generation method and device based on SM4 cryptographic algorithm
CN117873431A
Random number generating apparatus, random number generating method, program for generating random numbers, audio decoder and audio decoding method
US20030200240A1
Digital computation method involving euclidean division
US20070244956A1