A random number generation method, device and electronic device
Through multiple rounds of iterative processing based on computer system time, the time cluster factor is used to generate random numbers, which solves the unpredictability problem caused by the leakage of random number seeds in the existing technology, and achieves higher security and speed of random number generation.
Patent Information
- Application Number
- CN202311873500.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-12-30
- Publication Date
- 2024-09-17
- Estimated Expiration
- 2043-12-30
AI Technical Summary
Existing random number generation methods rely on random number seeds. Seed leakage can lead to random numbers being predicted, and adding anti-theft measures cannot completely avoid this risk, resulting in insufficient unpredictability.
By obtaining initial parameter values, multiple rounds of iterative processing are performed based on computer system time, time cluster factors are used to generate random numbers, avoiding the use of random number seeds, and the volatility of computer processing performance and computing speed is used to improve unpredictability.
It effectively improves the unpredictability of random number generation, avoids seed loss and security issues, and enhances the security and speed of random number generation.
Smart Images

Figure CN117827148B
Abstract
Description
Technical Field
[0001] The present application relates to the field of information security technology, and specifically discloses a random number generation method, device and electronic device. Background Art
[0002] Random numbers are widely used in information security tasks, such as data encryption, generating verification codes, etc. The current mainstream method of random number generation is achieved through hardware random number generators. Software random number generators are also widely used, and many methods for designing software random number generators have emerged. The design idea is generally to generate random numbers by performing function operations on a given random number seed (Rand Seed).
[0003] The random number seed is a necessary parameter introduced when the software random number generator performs function operations. Given the same random number seed in the random number generator, the same random number will be generated. Once the random number seed is leaked, the random number generated based on the random number seed can be inferred from the random number seed with a very high probability.
[0004] In order to improve the unpredictability of random number generation, a common approach is to add anti-theft measures to the storage of random number seeds, and prevent random numbers from being predicted by preventing seed leakage. However, adding anti-theft measures cannot completely avoid seed leakage, and there is still a risk that random numbers will be calculated due to seed leakage. In view of this, how to improve the unpredictability of random number generation is an issue that needs to be solved urgently. Summary of the invention
[0005] The embodiments of the present application provide a random number generation method, device and electronic device for improving the unpredictability of random number generation. The characteristic is that random number seeds are not used, which avoids seed loss from the root and also avoids the troubles that may be caused by ensuring seed security.
[0006] To achieve the above purpose, the technical solution of the embodiment of the present application is implemented as follows:
[0007] In a first aspect, an embodiment of the present application provides a method for generating a random number, the method comprising:
[0008] In response to the random number generation instruction, obtaining an initial parameter value;
[0009] In response to the random number generation instruction, obtaining an initial parameter value;
[0010] Performing multiple rounds of iterative processing on the system time of the computer based on the initial parameter value until an iterative end condition is met; determining a random number based on the time cluster factor obtained in the last round of iteration;
[0011] The process of each iteration is as follows:
[0012] The time factor and the function cumulative value of this round are calculated based on the preset function to obtain the time cluster factor of this round; the time factor in any round of iteration is the system time of the computer at the beginning of this round of iteration; the function cumulative value in the first round of iteration is the initial parameter value, and the function cumulative value in the non-first round of iteration is the time cluster factor obtained in the previous round of iteration.
[0013] In the embodiment of the present application, random numbers are generated without using random number seeds, which can just avoid seed loss and also avoid the trouble that may be caused to ensure seed safety. In addition, the time factor of each round of calculation in the embodiment of the present application is the system time of the computer at the beginning of the round of iteration. Due to the differences in the processing performance of different computers, it is difficult to deduce the time factor of each round of iteration even if the external parameters introduced by the first round of iteration are known, and the computing speed of the computer has a fluctuating characteristic, even if the amount of calculation of each round of iteration is the same, the computing time of each round of iteration will not be exactly the same. Under the premise that the external parameters introduced by the first round of iteration are obtained, even if a computer with the same performance as the computer generating the random number is used for simulation, it is difficult to exhaustively enumerate the time factor of each round of iteration. Therefore, the embodiment of the present application can effectively improve the unpredictability of random number generation.
[0014] In some possible embodiments, the iteration end condition is determined in the following manner:
[0015] Determining the number of iterations of the multiple iterations performed by the computer within a preset time period;
[0016] If the number of iteration rounds is greater than the first number of rounds, determining that the iteration end condition is that the iteration time reaches a preset time length;
[0017] If the number of iteration rounds is less than or equal to the first number of rounds, the iteration end condition is determined as the number of iteration rounds reaching a second number of rounds; wherein the first number of rounds and the second number of rounds are set according to the processing performance of the computer, and the second number of rounds is less than the first number of rounds.
[0018] In the embodiment of the present application, the specific number of iteration rounds that the computer can perform multiple rounds of iterative processing within a preset period of time is detected, and the iteration end condition of random number generation is determined according to the iteration round number. When the iteration round number is greater than the first round number, it indicates that the processing performance of the computer is high. At this time, the iteration time reaching the preset time can be selected as the iteration end condition to increase the random number generation rate and ensure the unpredictability of the random number. Correspondingly, when the iteration round number is not greater than the first round number, the iteration round number reaching the second round number can be selected as the iteration end condition to improve the security of random number generation.
[0019] In some possible embodiments, the determining the iteration end condition is that after the number of iterations reaches a second round number, the method further includes:
[0020] Determining the processing time for the computer to perform a second number of rounds of multiple iterative processing;
[0021] If the processing time is greater than a time threshold, the computational complexity of the preset function is reduced so that the processing time is less than or equal to the time threshold.
[0022] In the embodiment of the present application, the processing time required for the computer to perform multiple rounds of iterative processing of the second iteration round is detected, and according to the comparison result of the processing time and the time threshold, it is determined whether the rate at which the computer generates random numbers is too slow. If the processing time is greater than the time threshold, it indicates that the computer processing performance is slightly poor, which is likely to affect the random number generation rate requirement. At this time, the processing time can be shortened by reducing the computational complexity of the preset function, thereby increasing the random number generation rate.
[0023] In some possible embodiments, determining a random number according to the time cluster factor obtained in the last round of iterations includes:
[0024] In response to the random number requirement indication, obtaining a first data length of the random number requirement;
[0025] If the second data length of the time cluster factor obtained in the last round of iteration is the same as the first data length, the time cluster factor is used as the random number.
[0026] In an embodiment of the present application, a random number can be generated according to the required data length based on the time cluster factor obtained after multiple rounds of iterative processing. When the first data length required for the random number is the same as the second data length of the time cluster factor, the time cluster factor can be directly used as the random number.
[0027] In some possible embodiments, the method further includes:
[0028] If the second data length is greater than the first data length, intercepting data of the first data length from the time cluster factor obtained in the last round of iteration as the random number;
[0029] If the second data length is less than the first data length, performing the multiple rounds of iterative processing multiple times to obtain a newly generated time cluster factor each time the multiple rounds of iterative processing are performed, until the sum of the data length of the time cluster factor and the newly generated time cluster factor is not less than the first data length;
[0030] The random number is determined according to the time clustering factor and the newly generated time clustering factor.
[0031] In the embodiment of the present application, a random number that meets the required length can be adaptively generated based on the comparison result of the first data length of the random number requirement and the second data length of the time cluster factor. When the second data length of the time cluster factor is greater than the first data length of the random number requirement, data that meets the length requirement is intercepted from the time cluster factor as a random number. Correspondingly, when the second data length is less than the first data length, multiple rounds of iterative processing procedures are performed multiple times so that the sum of the data lengths of the first generated time cluster factor and the newly generated time cluster factor is not less than the first data length, and then a random number is obtained based on the first generated time cluster factor and the newly generated time cluster factor.
[0032] In some possible embodiments, determining the random number according to the time cluster factor and the newly generated time cluster factor includes:
[0033] From the data concatenation result of the time cluster factor and the newly generated time cluster factor, data of the first data length is intercepted as the random number.
[0034] In an embodiment of the present application, the time cluster factor generated for the first time and the newly generated time cluster factor are spliced so that the data splicing result can meet the first data length required for the random number, and then data of the first data length is cut out from the data splicing result as the random number.
[0035] In some possible embodiments, the minimum timing unit of the system time is microsecond.
[0036] In an embodiment of the present application, by setting the minimum timing unit of the system time used to generate random numbers to microseconds, the fluctuation characteristics of the computer's computing speed are fully utilized, so that the time factors introduced in each round of iteration are difficult to be accurately calculated or exhausted, thereby improving the unpredictability of random number generation.
[0037] In a second aspect, an embodiment of the present application provides a random number generation device, the device comprising:
[0038] a data acquisition unit, configured to acquire an initial parameter value in response to a random number generation indication;
[0039] The data generating unit is configured to perform multiple rounds of iterative processing on the system time of the computer based on the initial parameter value until the iteration end condition is met; determine the random number according to the time cluster factor obtained in the last round of iteration; wherein each round of iteration process is as follows:
[0040] The time factor and the function cumulative value of this round are calculated based on the preset function to obtain the time cluster factor of this round; the time factor in any round of iteration is the system time of the computer at the beginning of this round of iteration; the function cumulative value in the first round of iteration is the initial parameter value, and the function cumulative value in the non-first round of iteration is the time cluster factor obtained in the previous round of iteration.
[0041] In some possible embodiments, the iteration end condition is determined in the following manner:
[0042] Determining the number of iterations of the multiple iterations performed by the computer within a preset time period;
[0043] If the number of iteration rounds is greater than the first number of rounds, determining that the iteration end condition is that the iteration time reaches a preset time length;
[0044] If the number of iteration rounds is less than or equal to the first number of rounds, the iteration end condition is determined as the number of iteration rounds reaching a second number of rounds; wherein the first number of rounds and the second number of rounds are set according to the processing performance of the computer, and the second number of rounds is less than the first number of rounds.
[0045] In some possible embodiments, after performing the step of determining that the iteration end condition is that the number of iterations reaches a second round number, the data generating unit is further configured to:
[0046] Determining the processing time for the computer to perform a second number of rounds of multiple iterative processing;
[0047] If the processing time is greater than a time threshold, the computational complexity of the preset function is reduced so that the processing time is less than or equal to the time threshold.
[0048] In some possible embodiments, the determining of the random number according to the time cluster factor obtained in the last round of iteration is performed, and the data generating unit is specifically configured as follows:
[0049] In response to the random number requirement indication, obtaining a first data length of the random number requirement;
[0050] If the second data length of the time cluster factor obtained in the last round of iteration is the same as the first data length, the time cluster factor is used as the random number.
[0051] In some possible embodiments, the data generating unit is further configured to:
[0052] If the second data length is greater than the first data length, intercepting data of the first data length from the time cluster factor obtained in the last round of iteration as the random number;
[0053] If the second data length is less than the first data length, performing the multiple rounds of iterative processing multiple times to obtain a newly generated time cluster factor each time the multiple rounds of iterative processing are performed, until the sum of the data length of the time cluster factor and the newly generated time cluster factor is not less than the first data length;
[0054] The random number is determined according to the time clustering factor and the newly generated time clustering factor.
[0055] In some possible embodiments, the determining of the random number according to the time cluster factor and the newly generated time cluster factor is performed, and the data generating unit is specifically configured as follows:
[0056] From the data concatenation result of the time cluster factor and the newly generated time cluster factor, data of the first data length is intercepted as the random number.
[0057] In some possible embodiments, the minimum timing unit of the system time is microsecond.
[0058] In a third aspect, an embodiment of the present application provides an electronic device, including:
[0059] A memory for storing program instructions;
[0060] The processor is used to call the program instructions stored in the memory, and execute the steps included in any one of the methods in the first aspect according to the obtained program instructions.
[0061] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a computer, the computer executes any one of the methods described in the first aspect.
[0062] In a fifth aspect, an embodiment of the present application provides a computer program product, comprising: a computer program code, when the computer program code is run on a computer, the computer executes any one of the methods described in the first aspect.
[0063] In the embodiment of the present application, in response to the random number generation indication, the initial parameter value is obtained. Based on the initial parameter value, the system time of the computer is iterated for multiple rounds until the iteration end condition is met, and the random number is determined according to the time cluster factor obtained in the last round of iteration. In each round of iteration, the time factor of this round and the function cumulative value are calculated based on the preset function to obtain the time cluster factor of this round. In the above process, the time factor involved in each round of calculation is the system time of the computer at the beginning of the round of iteration. Due to the difference in processing performance of different computers, it is difficult to calculate the time factor of each round of iteration even if the external parameters introduced by the first round of iteration are known. In addition, the computing speed of the computer has a fluctuating characteristic. Even if the amount of calculation of each round of iteration is the same, the computing time of each round of iteration will not be exactly the same. Therefore, under the premise that the external parameters introduced by the first round of iteration are obtained, even if a computer with exactly the same performance as the computer generating the random number is used for calculation, it is difficult to exhaustively enumerate the time factor of each round of iteration. It can be seen that the embodiment of the present application can effectively improve the unpredictability of random number generation.
[0064] Other features and advantages of the present application will be described in the following description, and partly become apparent from the description, or be understood by practicing the present application. The purpose and other advantages of the present application can be realized and obtained by the structures specifically pointed out in the written description, claims, and drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0065] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0066] Figure 1 A schematic diagram of generating random numbers based on a random number seed provided in an embodiment of the present application;
[0067] Figure 2 An overall flow chart of a random number generation method provided in an embodiment of the present application;
[0068] Figure 3 A flowchart of multiple rounds of iterative processing provided in an embodiment of the present application;
[0069] Figure 4 A schematic diagram of computer fluctuations provided in an embodiment of the present application;
[0070] Figure 5 A flowchart for determining an iteration end condition provided in an embodiment of the present application;
[0071] Figure 6A structural diagram of a random number generation device provided in an embodiment of the present application;
[0072] Figure 7 A structural diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0073] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the technical solution of the present application, rather than all of the embodiments. Based on the embodiments recorded in the application documents, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the technical solution of the present application.
[0074] To facilitate understanding of the technical solution provided by this application, the following is a detailed description of the technical background:
[0075] As mentioned above, many random number generation methods are currently implemented through software-programmed random number generators. The basic path is to generate random numbers and seed updates by performing some operations on a given random number seed (Rand Seed) through a random number generator. For example, the Middle Square Method is to square a given random number seed and correct the square result to a preset length. Then, a specified length (for example, the middle 4 bits) is intercepted from the corrected square result as the generated random number.
[0076] Specific as Figure 1 As shown in the figure, given a random number seed of 2041, the random number seed is squared to obtain 4165681. Assuming the preset length is 8 bits, 0 can be added to the first bit to obtain the corrected 8-bit square result of 04165681. Then the middle 4 bits are intercepted to obtain the random number 1656 generated this time. Then the value 04165681, or the value after the transformation of 04165681, is used as the new seed.
[0077] From the above process, we can see that once the random number seed is leaked, the random number generated based on the random number seed can be inferred through the random number seed. The main prevention method at present is to add anti-theft measures to the storage of random number seeds, and prevent the random number from being predicted by preventing seed leakage. However, adding anti-theft measures cannot completely avoid seed leakage, and there is still a risk of random numbers being inferred due to seed leakage. In view of this, how to improve the unpredictability of random number generation is an urgent problem to be solved.
[0078] To solve the above problems, the inventive concept of the present application is as follows: in response to a random number generation indication, an initial parameter value is obtained, and the initial parameter value is a fixed constant selected by the user. Based on the initial parameter value, multiple rounds of iterative processing are performed on the system time of the computer until the iteration end condition is met, and the random number is determined according to the time cluster factor obtained in the last round of iteration. During each round of iteration, the time factor and the function cumulative value of this round are calculated based on a preset function to obtain the time cluster factor of this round. Among them, the time factor in any round of iteration is the system time of the computer at the beginning of this round of iteration; the function cumulative value in the first round of iteration is the initial parameter value, and the function cumulative value in the non-first round of iteration is the time cluster factor obtained in the previous round of iteration.
[0079] In the generation process of the above-mentioned random numbers, random number seeds are not used, which avoids seed loss from the root, and also avoids the trouble that may be caused to ensure seed safety. In addition, the time factor of each round of calculation in the embodiment of the present application is the system time of the computer at the beginning of the round of iteration. Due to the differences in the processing performance of different computers, it is difficult to deduce the time factor of each round of iteration even if the external parameters introduced by the first round of iteration are known, and the computing speed of the computer has a fluctuating characteristic, even if the amount of calculation of each round of iteration is the same, the computing time of each round of iteration will not be exactly the same. Under the premise that the external parameters introduced by the first round of iteration are obtained, even if a computer with the same performance as the computer generating the random number is used for simulation, it is difficult to exhaustively enumerate the time factor of each round of iteration. Therefore, the embodiment of the present application can effectively improve the unpredictability of random number generation.
[0080] Next, Figure 2 As shown, Figure 2 The overall process of a random number generation method provided by the present application is shown, which specifically includes the following steps:
[0081] Step 201: In response to a random number generation instruction, obtaining an initial parameter value;
[0082] The initial parameter value in the embodiment of the present application is randomly generated after a given data generation condition. For example, given a data condition of generating a 64-bit unsigned integer, when executing step 201, a 64-bit unsigned integer can be randomly generated as an initial parameter value for random number generation.
[0083] Specifically, 64-bit unsigned integers include {0,1,2,…,2 64 -1}. Using a randomly generated 64-bit unsigned integer as the initial parameter value is equivalent to starting from {0,1,2,…,2 64 -1} to determine an initial parameter value. A lazy approach is to always use zero.
[0084] Step 202: performing multiple rounds of iterative processing on the system time of the computer based on the initial parameter value until an iteration end condition is met; determining a random number according to the time cluster factor obtained in the last round of iteration;
[0085] Figure 3 The multi-round iterative process of this application is shown, such as Figure 3 As shown, the following steps are included:
[0086] Step 301: Determine the time factor and function cumulative value of this round;
[0087] The time factor in any round of iteration is the system time recorded by the computer at the beginning of the round of iteration. In implementation, the current system time of the computer can be read at the beginning of this round of iteration and used as the time factor. Currently, the system time that can be recorded by computers can reach microseconds or even nanoseconds. In specific implementation, the system time accurate to microseconds can be used as the time factor of this round. For example, the system time obtained instantly is 0 hours, 0 minutes, 0 seconds, 0 milliseconds, and 0 microseconds, which is less than 2 64 A positive integer can also be represented as a 64-bit binary number.
[0088] The cumulative value of the function in this round is determined according to the current iteration round. The cumulative value of the function in the first iteration round is the initial parameter value obtained in the above step 201, and the cumulative value of the function in the non-first iteration round is the time cluster factor output by the previous iteration round.
[0089] Step 302: Calculate the time factor of this round and the function cumulative value based on a preset function to obtain the time cluster factor of this round;
[0090] The preset function may include complex functions of various numerical calculations or logical operations such as rounding, modulus, and displacement. For example, the preset function may be as shown in the following formula (1):
[0091] F(A,t)=int[(A 5 / 3.14159265)+0.5]+((t<<<29)+257) 9 mod 2 64 (1)
[0092] Among them, F is the preset function, F(A,t) is the time cluster factor of this round, A is the cumulative value of the function of this round (also the time cluster factor calculated in the previous round), t is the time factor of this round, int is rounding, <<< is the shift operator, indicating that the 64-bit number is circularly shifted to the left, and mod is the remainder.
[0093] It should be noted that the above formula (1) is only an example of a preset function, and does not specifically limit the preset function of the embodiment of the present application. During the specific implementation, the preset function containing complex operations can be set according to actual needs. The purpose of requiring the preset function to contain complex operations, that is, to have a certain degree of complexity, is to improve the security of generating random numbers.
[0094] Step 303: Determine whether the iteration end condition is met;
[0095] The iteration end condition of the embodiment of the present application is set according to the processing performance of the computer. Specifically, the embodiment of the present application pre-sets three parameters for setting the iteration end condition from the perspectives of security, user experience, and applicable scenarios. These three setting parameters include: the minimum number of iterations N, the optimal generation time T1, and the maximum generation time T2.
[0096] To facilitate understanding of how this application determines the iteration end condition, the above three setting parameters are introduced separately below:
[0097] Minimum number of iterations N: This parameter is set from the perspective of security. As mentioned above, the computing speed of a computer has a fluctuating characteristic. Even if the amount of computing in each round of iteration is the same, the computing time of each round of iteration will not be exactly the same.
[0098] For example Figure 4 As shown in the figure, assuming that the time required for a computer to perform one round of iteration may fluctuate between 5 and 7 microseconds, then two iterations may produce 9 results, namely:
[0099] If the first iteration ends at 5 microseconds, the second iteration ends at 10 microseconds, 11 microseconds, and 12 microseconds (i.e., the second iteration takes 5 microseconds, 6 microseconds, or 7 microseconds);
[0100] If the first iteration ends at 6 microseconds, the second iteration ends at 11 microseconds, 12 microseconds, and 13 microseconds (i.e., the second iteration takes 5 microseconds, 6 microseconds, or 7 microseconds).
[0101] If the first iteration ends at 7 microseconds, the second iteration ends at 12 microseconds, 13 microseconds, and 14 microseconds (i.e., the second iteration takes 5 microseconds, 6 microseconds, or 7 microseconds). Figure 4 Nine results are shown.
[0102] To prevent prediction of random numbers by exhaustively enumerating all possibilities, the minimum number of iterations N can be set to 100, which means that at least 100 rounds of the above steps are required to generate a random number. Figure 3 The multi-round iterative processing flow is shown.
[0103] Optimal generation time T1: This parameter is set from the perspective of user experience. This parameter indicates the optimal time required to generate a random number. The optimal time expected by users can be obtained by conducting a questionnaire survey on users. For example, the optimal time T1 can be set to 100 microseconds.
[0104] Maximum generation time T2: This parameter is set from the perspective of application. According to statistics, generating 200 random numbers per second can meet the needs of most application scenarios.
[0105] Based on this, the maximum generation time T2 can be set to 1s / Num. Num represents the number of random numbers generated per second that can meet most application scenarios. For example, if Num is 200, then T2 = 1s / 200 = 5000 (microseconds). This means that if you want to generate 200 random numbers in 1 second, the maximum time required to generate a random number cannot exceed 5000 microseconds, otherwise it may not be able to meet the random number generation rate required by users.
[0106] After introducing the three setting parameters (N, T1 and T2) used for the decision iteration end condition in the embodiment of the present application through the above content, the following is Figure 5 This article describes how to determine the iteration end conditions for multiple rounds of iterative processing. Figure 5 As shown, the following steps are included:
[0107] Step 501: Determine the number of iteration rounds that the computer performs multiple iterations within a preset period of time;
[0108] The random number generation program is tested on the computer to calculate how many rounds of iteration can be performed within a preset time period t0 (1 second), that is, the number of iteration rounds n0 in step 501 is obtained.
[0109] It should be noted that the reason for calculating how many rounds of iterations can be performed in 1 second is that if the processing performance of the computer is relatively good, the calculation time of one round of iteration will usually end within a few microseconds or even 1 microsecond. However, due to hardware limitations, it is usually impossible to accurately capture the time consumption of a single iteration through a conventional computer. Therefore, the embodiment of the present application chooses to judge the processing performance of the computer by capturing the number of iteration rounds n0 of the computer in 1 second.
[0110] Step 502: Check whether the iteration round number n0 is greater than the first round number n1;
[0111] The following first explains how the first round number n1 is obtained:
[0112] The first round number n1 in the embodiment of the present application is obtained based on the above-mentioned optimal generation time T1 (100 above) and the minimum number of iterations N (100 rounds). As mentioned above, T1 represents the optimal time required to generate a random number, and N represents the minimum number of iterations required to generate a random number. Therefore, T1 / N=100 is the minimum number of rounds of a random number = 100, which means that within 100, a random number is generated by iterating 100 rounds, then the average time t required for each iteration round is i For the next life.
[0113] Next, convert the unit to 1 second by calculating t0 / t i Get how many rounds of iterations the computer can perform in 1 second, that is, how many rounds of iterations are performed in 1s / 1, that is, the first round number n1 is 1000000 rounds.
[0114] Step 503: If the iteration round number n0 is greater than the first round number n1, the iteration end condition is determined to be that the iteration time reaches the preset time length ti;
[0115] If the number of iteration rounds n0 is greater than the first number of rounds n1, it means that the processing performance of the computer is fast enough to satisfy 100 iterations within 100 seconds. Therefore, on the basis of ensuring security, the iteration end condition can be set based on the aforementioned optimal generation time T1. Specifically, the iteration end condition can be determined as the iteration time reaching a preset duration. The preset duration is set according to the aforementioned optimal generation time T1. For example, T1 can be directly set as the preset duration for determining the end of the iteration.
[0116] Step 504: if the iteration round number n0 is less than or equal to the first round number n1, then determining that the iteration end condition is that the iteration round number reaches the second round number n2;
[0117] If the number of iterations n0 is less than or equal to the first number of iterations n1, it means that the computer cannot meet the requirement of 100 iterations within 100 minutes. At this time, the security of the random number should be guaranteed. Specifically, the iteration end condition can be set to the number of iterations reaching the second number of iterations n2. During implementation, the minimum number of iterations N mentioned above can be set to the second number of iterations n2. That is, it is guaranteed that each random number is generated after 100 iterations.
[0118] In order to ensure that the random number generation rate can meet user needs, after the iteration end condition is determined to be that the number of iterations reaches the second round number n2, it is necessary to obtain the processing time t1 consumed by the computer to execute n2 rounds of multi-round iterative processing.
[0119] If the processing time t1 is greater than the time threshold △t, it means that the processing speed of this computer is very low. The current preset function cannot generate more than 200 random numbers per second under the premise of ensuring security (i.e., at least 100 iterations), that is, the random number generation rate cannot meet user needs. △t is determined based on the above maximum generation time T2. For example, T2 can be directly set to △t.
[0120] At this time, it is necessary to reduce the computational complexity of the preset function so that the processing time t1 is less than or equal to the time threshold Δt. In implementation, the computational complexity can be reduced by reducing the number of terms involved in the computation in the preset function or reducing the power of the power operation, etc., which is not limited in this application.
[0121] It should be noted that the operation of reducing the computational complexity of the preset function here is equivalent to updating the preset function, and the updated preset function needs to be used to generate random numbers in the subsequent generation of random numbers.
[0122] Step 304: If the iteration end condition is met, the iteration ends and a random number is determined according to the time cluster factor obtained in the last round of iteration; otherwise, return to step 301 to execute the next round of iteration.
[0123] In actual applications, users may have requirements for the length of random numbers, for example, they may require the generation of 32-bit, 64-bit, 128-bit or even 256-bit random numbers.
[0124] When executing step 304, it is first determined whether the user has a requirement for the length of the random number. If not, the time cluster factor obtained in the last round of iteration can be directly used as the generated random number.
[0125] If there is a length requirement, a first data length L1 of the random number requirement and a second data length L2 of the time cluster factor obtained in the last round of iteration are obtained.
[0126] If L1=L2, it means that the time cluster factor just meets the random number length required by the user. At this time, the time cluster factor obtained in the last round of iteration can be directly used as the generated random number.
[0127] If L2>L1, it means that the currently generated time cluster factor is too long. In this case, the data of length L1 can be intercepted from the time cluster factor obtained in the last round of iteration as a random number. For example, the time cluster factor a obtained in the last round is 64 bits, and a1, a2, a3, ... a 64 Assuming that the second data length required by the user is 16 bits, a1 to a1 can be intercepted from left to right. 16 As the random number generated this time, you can also intercept a in the middle 17 ~a 32As the random number generated this time. The embodiment of the present application does not limit the method of intercepting data of the time cluster factor, and a random interception method can be adopted during implementation, and it is only necessary to ensure that data of length L1 is intercepted.
[0128] If L2<L1, it means that the currently generated time cluster factor is too short. In this case, multiple rounds of iterations may be performed to splice the multiple time cluster factors obtained so that the spliced time cluster factor reaches the first data length L1 required by the user.
[0129] During implementation, the above-mentioned multiple rounds of iterative processing flow can be executed multiple times to generate a new time cluster factor after each execution of the multiple rounds of iterative processing flow, until the sum of the data lengths of the time cluster factor generated this time and the newly generated time cluster factor is not less than the first data length L1 required by the user.
[0130] For example, during the multi-round iterative processing, the data length of the time cluster factor generated each time is 64 bits, and the first data length L1 required by the user is 100 bits, then two multi-round iterative processing processes need to be performed to generate two 64-bit time cluster factors. That is, the sum of the data lengths of the time cluster factor generated this time and the newly generated time cluster factor (128 bits) is greater than the first data length L1 (100 bits).
[0131] Next, from the data concatenation result of the time cluster factor and the newly generated time cluster factor, data of the first data length is intercepted as a random number. Continuing with the above example, in implementation, the time cluster factor generated this time and the newly generated time cluster factor can be concatenated to obtain a 128-bit data concatenation result, and then 100 bits of data are intercepted from the 128-bit data concatenation result as the final generated random number, thereby obtaining a random number of a length that meets the user's requirements.
[0132] In the embodiment of the present application, the parameters of each round of the preset function are the cumulative value of the function and the time factor of the round, and the time factor involved in the calculation of each round is the system time of the computer at the beginning of the round of iteration, which does not need to be pre-set and stored. Therefore, the random number generation process of the present application does not actually require the participation of the random number seed, that is, the possibility of random number deduction due to the leakage of the random number seed is avoided from the source.
[0133] Furthermore, due to the differences in processing performance of different computers, it is difficult to deduce the time factor of each round of iteration even if the external parameters introduced by the first round of iteration are known. In addition, the computing speed of the computer has a fluctuating characteristic. Even if the amount of calculation of each round of iteration is the same, the computing time of each round of iteration will not be exactly the same. Therefore, under the premise that the external parameters introduced by the first round of iteration are obtained, even if a computer with exactly the same performance as the computer generating the random number is used for calculation, it is difficult to exhaustively enumerate the time factor of each round of iteration. It can be seen from this that the embodiments of the present application can effectively improve the unpredictability of random number generation.
[0134] Based on the same inventive concept, the present application also provides a random number generating device, specifically as follows: Figure 6 As shown, including:
[0135] The data acquisition unit 601 is configured to acquire an initial parameter value in response to a random number generation indication;
[0136] The data generating unit 602 is configured to perform multiple rounds of iterative processing on the system time of the computer based on the initial parameter value until the iteration end condition is met; determine the random number according to the time cluster factor obtained in the last round of iteration; wherein each round of iteration process is as follows:
[0137] The time factor and the function cumulative value of this round are calculated based on the preset function to obtain the time cluster factor of this round; the time factor in any round of iteration is the system time of the computer at the beginning of this round of iteration; the function cumulative value in the first round of iteration is the initial parameter value, and the function cumulative value in the non-first round of iteration is the time cluster factor obtained in the previous round of iteration.
[0138] In some possible embodiments, the iteration end condition is determined in the following manner:
[0139] Determining the number of iterations of the multiple iterations performed by the computer within a preset time period;
[0140] If the number of iteration rounds is greater than the first number of rounds, determining that the iteration end condition is that the iteration time reaches a preset time length;
[0141] If the number of iteration rounds is less than or equal to the first number of rounds, the iteration end condition is determined as the number of iteration rounds reaching a second number of rounds; wherein the first number of rounds and the second number of rounds are set according to the processing performance of the computer, and the second number of rounds is less than the first number of rounds.
[0142] In some possible embodiments, after determining that the iteration end condition is that the number of iterations reaches a second round number, the data generating unit 602 is further configured to:
[0143] Determining the processing time for the computer to perform a second number of rounds of multiple iterative processing;
[0144] If the processing time is greater than a time threshold, the computational complexity of the preset function is reduced so that the processing time is less than or equal to the time threshold.
[0145] In some possible embodiments, the determining of the random number according to the time cluster factor obtained in the last round of iteration is performed, and the data generating unit 602 is specifically configured as follows:
[0146] In response to the random number requirement indication, obtaining a first data length of the random number requirement;
[0147] If the second data length of the time cluster factor obtained in the last round of iteration is the same as the first data length, the time cluster factor is used as the random number.
[0148] In some possible embodiments, the data generating unit 602 is further configured to:
[0149] If the second data length is greater than the first data length, intercepting data of the first data length from the time cluster factor obtained in the last round of iteration as the random number;
[0150] If the second data length is less than the first data length, performing the multiple rounds of iterative processing multiple times to obtain a newly generated time cluster factor each time the multiple rounds of iterative processing are performed, until the sum of the data length of the time cluster factor and the newly generated time cluster factor is not less than the first data length;
[0151] The random number is determined according to the time clustering factor and the newly generated time clustering factor.
[0152] In some possible embodiments, the determining of the random number according to the time cluster factor and the newly generated time cluster factor is performed, and the data generating unit 602 is specifically configured as follows:
[0153] From the data concatenation result of the time cluster factor and the newly generated time cluster factor, data of the first data length is intercepted as the random number.
[0154] In some possible embodiments, the minimum timing unit of the system time is microsecond.
[0155] Refer to the following Figure 7 The electronic device 130 according to this embodiment of the present application is described. Figure 7 The electronic device 130 shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0156] like Figure 7 As shown, the electronic device 130 is in the form of a general electronic device. The components of the electronic device 130 may include but are not limited to: the at least one processor 131, the at least one memory 132, and a bus 133 connecting different system components (including the memory 132 and the processor 131).
[0157] Bus 133 represents one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, a processor, or a local bus using any of a variety of bus architectures.
[0158] The memory 132 may include a readable medium in the form of a volatile memory, such as a random access memory (RAM) 1321 and / or a cache memory 1322 , and may further include a read-only memory (ROM) 1323 .
[0159] The memory 132 may also include a program / utility 1325 having a set (at least one) of program modules 1324, such program modules 1324 including but not limited to: an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment.
[0160] The electronic device 130 may also communicate with one or more external devices 134 (e.g., keyboards, pointing devices, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 130, and / or communicate with any device that enables the electronic device 130 to communicate with one or more other electronic devices (e.g., routers, modems, etc.). Such communication may be performed via an input / output (I / O) interface 135. Furthermore, the electronic device 130 may also communicate with one or more networks (e.g., a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) via a network adapter 136. As shown, the network adapter 136 communicates with other modules for the electronic device 130 via a bus 133. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 130, including but not limited to: microcode, device drivers, redundant processors, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0161] In an exemplary embodiment, a computer-readable storage medium including instructions is also provided, such as a memory 132 including instructions, and the instructions can be executed by the processor 131 of the above device to complete the above method. Alternatively, the computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, an optical data storage device, etc.
[0162] In an exemplary embodiment, a computer program product is also provided, including a computer program / instruction, and when the computer program / instruction is executed by the processor 131, any method of a random number generation method provided in the present application is implemented.
[0163] In an exemplary embodiment, various aspects of a random number generation method provided by the present application may also be implemented in the form of a program product, which includes a program code. When the program product is run on a computer device, the program code is used to enable the computer device to execute the steps of a random number generation method according to various exemplary embodiments of the present application described above in this specification.
[0164] The program product may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination thereof. More specific examples of readable storage media (a non-exhaustive list) include: an electrical connection with one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof.
[0165] The program product for random number generation of the embodiment of the present application can adopt a portable compact disk read-only memory (CD-ROM) and include program code, and can be run on an electronic device. However, the program product of the present application is not limited to this. In this document, a readable storage medium can be any tangible medium containing or storing a program, which can be used by an instruction execution system, an apparatus or a device or used in combination with it.
[0166] A readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, wherein readable program code is carried. Such propagated data signals may take a variety of forms, including, but not limited to, electromagnetic signals, optical signals, or any suitable combination of the foregoing. A readable signal medium may also be any readable medium other than a readable storage medium, which may transmit, propagate, or transfer a program for use by or in conjunction with an instruction execution system, apparatus, or device.
[0167] The program code embodied on the readable medium may be transmitted using any appropriate medium, including but not limited to wireless, wireline, optical fiber cable, RF, etc., or any suitable combination of the foregoing.
[0168] The program code for performing the operation of the present application can be written in any combination of one or more programming languages, including object-oriented programming languages such as Java, C++, etc., and also conventional procedural programming languages such as language or similar programming languages. The program code can be executed entirely on the user electronic device, partially on the user device, as an independent software package, partially on the user electronic device and partially on the remote electronic device, or completely on the remote electronic device or server. In the case of a remote electronic device, the remote electronic device can be connected to the user electronic device through any type of network, including a local area network (LAN) or a wide area network (WAN), or can be connected to an external electronic device (for example, using an Internet service provider to connect through the Internet).
[0169] It should be noted that, although several units or subunits of the device are mentioned in the above detailed description, this division is merely exemplary and not mandatory. In fact, according to the embodiments of the present application, the features and functions of two or more units described above can be embodied in one unit. Conversely, the features and functions of one unit described above can be further divided into multiple units to be embodied.
[0170] In addition, although the operations of the method of the present application are described in a specific order in the drawings, this does not require or imply that the operations must be performed in this specific order, or that all the operations shown must be performed to achieve the desired results. Additionally or alternatively, some steps may be omitted, multiple steps may be combined into one step, and / or one step may be decomposed into multiple steps.
[0171] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.
[0172] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. 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 image scaling device to produce a machine, so that the instructions executed by the processor of the computer or other programmable image scaling device generate instructions for implementing the processes in the flowchart and / or block diagram. 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.
[0173] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable image scaling device to operate in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture including an instruction device, the instruction device being implemented in the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0174] These computer program instructions may also be loaded onto a computer or other programmable image scaling device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, whereby the instructions executed on the computer or other programmable device provide for implementing 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.
[0175] Although the preferred embodiments of the present application 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 falling within the scope of the present application.
[0176] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A random number generation method, characterized in that: The method comprises: In response to the random number generation instruction, obtaining an initial parameter value; Performing multiple rounds of iterative processing on the system time of the computer based on the initial parameter value until an iterative end condition is met; determining a random number based on the time cluster factor obtained in the last round of iteration; The process of each iteration is as follows: The time factor and the function cumulative value of this round are calculated based on the preset function to obtain the time cluster factor of this round; the time factor in any round of iteration is the system time of the computer at the beginning of this round of iteration; the function cumulative value in the first round of iteration is the initial parameter value, and the function cumulative value in the non-first round of iteration is the time cluster factor obtained by the previous round of iteration; the iteration end condition is that the current iteration round number is greater than the specified round number; the specified round number is determined according to the processing performance of the computer.
2. The method according to claim 1, characterized in that The specified number of rounds includes the first number of rounds or the second number of rounds, and the iteration end condition is determined in the following manner: Determining the number of iterations of the multiple iterations performed by the computer within a preset time period; If the number of iteration rounds is greater than the first number of rounds, determining that the iteration end condition is that the iteration time reaches a preset time length; If the number of iteration rounds is less than or equal to the first number of rounds, the iteration end condition is determined as the number of iteration rounds reaching a second number of rounds; wherein the first number of rounds and the second number of rounds are set according to the processing performance of the computer, and the second number of rounds is less than the first number of rounds.
3. The method according to claim 2, characterized in that The determining that the iteration end condition is after the number of iterations reaches a second round number, the method further includes: Determining the processing time for the computer to perform a second number of rounds of multiple iterative processing; If the processing time is greater than a time threshold, the computational complexity of the preset function is reduced so that the processing time is less than or equal to the time threshold.
4. The method according to claim 1, characterized in that: The step of determining a random number based on the time cluster factor obtained in the last round of iterations includes: In response to the random number requirement indication, obtaining a first data length of the random number requirement; If the second data length of the time cluster factor obtained in the last round of iteration is the same as the first data length, the time cluster factor is used as the random number.
5. The method according to claim 4, characterized in that The method further comprises: If the second data length is greater than the first data length, intercepting data of the first data length from the time cluster factor obtained in the last round of iteration as the random number; If the second data length is less than the first data length, performing the multiple rounds of iterative processing multiple times to obtain a newly generated time cluster factor each time the multiple rounds of iterative processing are performed, until the sum of the data length of the time cluster factor and the newly generated time cluster factor is not less than the first data length; The random number is determined according to the time clustering factor and the newly generated time clustering factor.
6. The method according to claim 5, characterized in that The determining the random number according to the time cluster factor and the newly generated time cluster factor comprises: From the data concatenation result of the time cluster factor and the newly generated time cluster factor, data of the first data length is intercepted as the random number.
7. The method according to any one of claims 1 to 6, characterized in that: The minimum timing unit of the system time is microsecond.
8. A random number generating device, characterized in that: The device comprises: a data acquisition unit, configured to acquire an initial parameter value in response to a random number generation indication; The data generating unit is configured to perform multiple rounds of iterative processing on the system time of the computer based on the initial parameter value until the iteration end condition is met; determine the random number according to the time cluster factor obtained in the last round of iteration; wherein each round of iteration process is as follows: The time factor and the function cumulative value of this round are calculated based on the preset function to obtain the time cluster factor of this round; the time factor in any round of iteration is the system time of the computer at the beginning of this round of iteration; the function cumulative value in the first round of iteration is the initial parameter value, and the function cumulative value in the non-first round of iteration is the time cluster factor obtained by the previous round of iteration; the iteration end condition is that the current iteration round number is greater than the specified round number; the specified round number is determined according to the processing performance of the computer.
9. The device according to claim 8, characterized in that The specified number of rounds includes the first number of rounds or the second number of rounds, and the iteration end condition is determined in the following manner: Determining the number of iterations of the multiple iterations performed by the computer within a preset time period; If the number of iteration rounds is greater than the first number of rounds, determining that the iteration end condition is that the iteration time reaches a preset time length; If the number of iteration rounds is less than or equal to the first number of rounds, the iteration end condition is determined as the number of iteration rounds reaching a second number of rounds; wherein the first number of rounds and the second number of rounds are set according to the processing performance of the computer, and the second number of rounds is less than the first number of rounds.
10. The device according to claim 9, characterized in that After executing the step of determining that the iteration end condition is that the number of iterations reaches a second round number, the data generating unit is further configured to: Determining the processing time for the computer to perform a second number of rounds of multiple iterative processing; If the processing time is greater than a time threshold, the computational complexity of the preset function is reduced so that the processing time is less than or equal to the time threshold.
11. The device according to claim 8, characterized in that Executing the determination of the random number according to the time cluster factor obtained in the last round of iteration, the data generation unit is specifically configured as follows: In response to the random number requirement indication, obtaining a first data length of the random number requirement; If the second data length of the time cluster factor obtained in the last round of iteration is the same as the first data length, the time cluster factor is used as the random number.
12. The device according to claim 11, characterized in that The data generating unit is further configured to: If the second data length is greater than the first data length, intercepting data of the first data length from the time cluster factor obtained in the last round of iteration as the random number; If the second data length is less than the first data length, performing the multiple rounds of iterative processing multiple times to obtain a newly generated time cluster factor each time the multiple rounds of iterative processing are performed, until the sum of the data length of the time cluster factor and the newly generated time cluster factor is not less than the first data length; The random number is determined according to the time clustering factor and the newly generated time clustering factor.
13. The device according to claim 12, characterized in that To perform the step of determining the random number according to the time cluster factor and the newly generated time cluster factor, the data generating unit is specifically configured as follows: From the data concatenation result of the time cluster factor and the newly generated time cluster factor, data of the first data length is intercepted as the random number.
14. The device according to any one of claims 8 to 13, characterized in that: The minimum timing unit of the system time is microsecond.
15. An electronic device, characterized in that: include: A memory for storing program instructions; A processor is used to call the program instructions stored in the memory, and execute the steps included in any one of claims 1-7 according to the obtained program instructions.
16. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a computer, the computer executes the method according to any one of claims 1 to 7.
