Method and device for generating random number, equipment and storage medium
By dividing the storage length of random numbers into two parts, generating and combining the number set, the problem of inefficient random number generation is solved, and a random number generation method that efficiently generates and saves storage space is realized.
Patent Information
- Application Number
- CN202410105484.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-24
- Publication Date
- 2025-08-01
AI Technical Summary
When the range of random numbers required by users is large, the efficiency of generating random numbers in the prior art is inefficient and storage overhead is large, and repeated verification of newly generated random numbers and a large number of used random numbers is required.
The storage length of the random numbers to be generated is divided into a first storage length and a second storage length, a first and second set of numbers are generated, and the random numbers are generated by combining these numbers to avoid repeated verification, and only the first and second sets of numbers are stored.
It improves the efficiency of random numbers, reduces storage space usage, reduces storage overhead, and ensures that the generated random numbers are not duplicated.
Smart Images

Figure CN120406899A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of computer technology, and in particular, to a method, device, equipment, and storage medium for generating random numbers. Background Art
[0002] Currently, in the field of computer technology, random numbers can play an identification role. For example, random numbers can be used to identify the device code of an electronic device, or to identify the screen sharing code between electronic devices.
[0003] In the related art, a random number generator can generate random numbers for users according to the range of random numbers required by the users. In the related art, in order to make the generated random numbers non-repetitive, the used random numbers can be stored in a database, and the newly generated random numbers can be compared with the used random numbers in the database to determine whether the newly generated random numbers are duplicate generated random numbers. Thus, duplicate verification of the newly generated random numbers is performed based on the database.
[0004] However, when the range of random numbers required by the users is large, a large number of used random numbers may be stored in the database. In this way, when performing duplicate verification of the newly generated random numbers based on the database, the newly generated random numbers need to be compared with a large number of used random numbers in the database, resulting in a long time-consuming for generating random numbers. Thus, the generation efficiency of random numbers is affected. Summary of the Invention
[0005] Embodiments of the present disclosure provide a method, device, equipment, and storage medium for generating random numbers, which can improve the generation efficiency of random numbers.
[0006] In a first aspect, an embodiment of the present disclosure provides a method for generating random numbers, including:
[0007] Determine a first storage length and a second storage length according to the storage length of the random number to be generated;
[0008] Generate a first set of numbers according to the first storage length; and generate a second set of numbers according to the second storage length;
[0009] Select the numbers in the first set of numbers in sequence, and combine them with all the numbers in the second set of numbers respectively to obtain random numbers;
[0010] wherein, the storage length of the random number is equal to the sum of the first storage length and the second storage length; the first storage length is the storage length of each number in the first set of numbers, and the second storage length is the storage length of each number in the second set of numbers.
[0011] Further, a first set of numbers is generated according to the first storage length, including: determining a first value range according to the first storage length; generating a first set of numbers based on the first value range; wherein the first set of numbers includes all the numbers within the first value range.
[0012] Further, a second set of numbers is generated according to the second storage length, including: determining a second value range according to the second storage length; generating a second set of numbers based on the second value range; wherein the second set of numbers includes all the numbers within the second value range.
[0013] Further, numbers in the first set of numbers are sequentially selected and combined with all the numbers in the second set of numbers respectively to obtain random numbers, including: when the storage length of the first number is greater than the storage length of the second number, expanding the storage length of the second number to the first storage length; calculating the exclusive OR operation result between the first number and the expanded second number; combining the exclusive OR operation result with the second number to obtain a random number; wherein the first number is a number sequentially selected from the first set of numbers, and the second number is any number in the second set of numbers.
[0014] Further, numbers in the first set of numbers are sequentially selected and combined with all the numbers in the second set of numbers respectively to obtain random numbers, including: when the storage length of the first number is equal to the storage length of the second number, calculating the exclusive OR operation result between the first number and the second number; combining the exclusive OR operation result with the first number or the second number to obtain a random number; wherein the first number is a number sequentially selected from the first set of numbers, and the second number is any number in the second set of numbers.
[0015] Optionally, the method further includes: obtaining a generation request for a random number to be generated; wherein the generation request for the random number to be generated carries the value range of the random number to be generated; determining the storage length of the random number to be generated according to the value range of the random number to be generated.
[0016] Optionally, after obtaining the random number, the method further includes: converting the random number into a decimal format to form a random sequence including a plurality of decimal random numbers.
[0017] In a second aspect, an embodiment of the present disclosure provides an apparatus for generating random numbers, including:
[0018] A determination module, configured to determine a first storage length and a second storage length according to the storage length of the random number to be generated;
[0019] A generation module, configured to generate a first set of numbers according to the first storage length; and generate a second set of numbers according to the second storage length;
[0020] A combination module is configured to sequentially select numbers from a first set of numbers and combine them with all the numbers in a second set of numbers respectively to obtain random numbers;
[0021] wherein, the storage length of the random numbers is equal to the sum of the first storage length and the second storage length; the first storage length is the storage length of each number in the first set of numbers, and the second storage length is the storage length of each number in the second set of numbers.
[0022] In a third aspect, an embodiment of the present disclosure provides an electronic device, including a processor and a memory storing program instructions, wherein the processor is configured to execute the method for generating random numbers according to the first aspect when the program instructions are running.
[0023] In a fourth aspect, an embodiment of the present disclosure provides a storage medium storing program instructions, wherein the program instructions execute the method for generating random numbers according to the first aspect when running.
[0024] The method, apparatus, device, and storage medium for generating random numbers provided by the embodiments of the present disclosure can achieve the following technical effects:
[0025] Since the working principle of a computer is to perform operations using a binary algorithm generated by high and low levels, in the embodiments of the present disclosure, when generating random numbers, for the convenience of operation, the random numbers are in binary format. Specifically, when generating random numbers, the storage length of the random numbers to be generated can be divided into a first storage length and a second storage length, and the sum of the first storage length and the second storage length is the storage length of the binary random numbers. According to the first storage length, a first set of numbers can be generated, and the storage length of each number in the first set of numbers is the first storage length. And according to the second storage length, a second set of numbers can be generated, and the storage length of each number in the second set of numbers is the second storage length. Sequentially select numbers from the first set of numbers and combine them with all the numbers in the second set of numbers respectively to obtain random numbers. In the embodiments of the present disclosure, since the storage length of the random numbers to be generated is the sum of the first storage length and the second storage length, the numbers with the first storage length in the first set of numbers and the numbers with the second storage length in the second set of numbers can be combined to generate random numbers with a storage length consistent with that of the random numbers to be generated, so that the generated random numbers meet the user's requirements. Moreover, after sequentially selecting numbers from the first set of numbers and combining them with all the numbers in the second set of numbers respectively, the combination methods obtained are unique and non-repetitive, which can ensure that the combined random numbers are also non-repetitive. In this way, in the embodiments of the present disclosure, during the process of generating random numbers, it is not necessary to perform repeated verification on the generated random numbers. Therefore, when the range of random numbers required by the user is relatively large, the generation efficiency of random numbers can be improved.
[0026] The above general description and the following description are only exemplary and explanatory, and are not used to limit this application. Description of the Drawings
[0027] One or more embodiments are exemplarily illustrated by corresponding drawings. These exemplary illustrations and the drawings do not constitute a limitation on the embodiments. Elements with the same reference numerals in the drawings are shown as similar elements. The drawings do not constitute a scale limitation, and among them:
[0028] Figure 1 is a schematic diagram of the environment applicable to a method for generating random numbers provided by an embodiment of the present disclosure;
[0029] Figure 2 is a flowchart of a method for generating random numbers provided by an embodiment of the present disclosure;
[0030] Figure 3 is a flowchart of directly combining a first number and a second number to generate a random number provided by an embodiment of the present disclosure;
[0031] Figure 4 is a flowchart of encrypting and combining a first number and a second number to generate a random number provided by an embodiment of the present disclosure;
[0032] Figure 5 is a schematic diagram of another method for generating random numbers provided by an embodiment of the present disclosure;
[0033] Figure 6 is a schematic diagram of another method for generating random numbers provided by an embodiment of the present disclosure;
[0034] Figure 7 is a schematic diagram of a device for generating random numbers provided by an embodiment of the present disclosure;
[0035] Figure 8 is a schematic diagram of an electronic device provided by an embodiment of the present disclosure. Detailed Description of the Embodiments
[0036] In order to be able to understand the features and technical content of the embodiments of the present disclosure in more detail, the implementation of the embodiments of the present disclosure will be described in detail below in conjunction with the drawings. The attached drawings are only for reference and illustration, and are not used to limit the embodiments of the present disclosure. In the following technical description, for the sake of explanation, sufficient understanding of the disclosed embodiments is provided through multiple details. However, one or more embodiments can still be implemented without these details. In other cases, well-known structures and devices can be shown in a simplified manner to simplify the drawings.
[0037] In the description, claims, and the above-mentioned drawings of the embodiments of the present disclosure, terms such as "first" and "second" are used to distinguish similar objects and do not necessarily describe a specific order or sequence. It should be understood that the data used in this way can be interchanged under appropriate circumstances so as to implement the embodiments of the present disclosure described herein. In addition, the terms "comprising" and "having" and any variations thereof are intended to cover non-exclusive inclusion.
[0038] Unless otherwise specified, the term "plurality" means two or more.
[0039] In the embodiments of the present disclosure, the character " / " indicates that the objects before and after are in an "or" relationship. For example, A / B means: A or B.
[0040] The term "and / or" is an associative relationship describing an object and indicates that three relationships can exist. For example, A and / or B means: A or B, or, the three relationships of A and B.
[0041] The term "corresponding" may refer to an associative relationship or a binding relationship. That A corresponds to B means that there is an associative relationship or a binding relationship between A and B.
[0042] Currently, in the field of computer technology, random numbers can play an identification role. For example, a random number can be used to identify the device code of an electronic device, or a random number can be used to identify the screen sharing code between electronic devices.
[0043] In the related art, random numbers are usually generated according to the random number range required by the user for the user to use, and the random numbers that have been used by the user are stored in a database. In order to ensure that the generated random numbers are not repeated, when generating random numbers, the newly generated random numbers are compared with the random numbers that have been used in the database to determine whether the newly generated random numbers are duplicate generated random numbers. Thus, duplicate verification of the newly generated random numbers is performed based on the database.
[0044] However, when the random number range required by the user is large, a large number of random numbers that have been used by the user may be stored in the database. In this way, when performing duplicate verification of the newly generated random numbers based on the database, the newly generated random numbers need to be compared with a large number of random numbers that have been used by the user in the database, resulting in a long time-consuming for generating random numbers. Thus, the generation efficiency of random numbers is affected.
[0045] In addition, when the random number range required by the user is large and the numbers within the random number range are about to be exhausted, in order to perform duplicate verification of the newly generated random numbers, a large number of random numbers that have been used by the user are stored in the database. In this way, during the process of generating random numbers, a large amount of storage space is occupied to store the random numbers used by the user, increasing the storage overhead.
[0046] In view of this, embodiments of the present disclosure provide a method, apparatus, device, and storage medium for generating random numbers. When generating random numbers, the storage length of the random numbers to be generated can be divided into a first storage length and a second storage length. A first set of numbers is generated according to the first storage length, and a second set of numbers is generated according to the second storage length. Since the storage length of the random numbers to be generated is the sum of the first storage length and the second storage length, by sequentially selecting the numbers with the first storage length in the first set of numbers and the numbers with the second storage length in the second set of numbers for combination, random numbers with a storage length consistent with that of the random numbers to be generated can be generated, so that the generated random numbers meet the user's requirements. Moreover, after sequentially selecting the numbers in the first set of numbers and combining them with all the numbers in the second set of numbers respectively, the combination methods are unique and non-repetitive, which can ensure that the combined random numbers are also non-repetitive. In addition, when generating random numbers in embodiments of the present disclosure, only the first set of numbers and the second set of numbers need to be stored. Since the newly generated random numbers do not need to be repeatedly verified, there is no need to store the random numbers that have been used by the user within the random number range. Thus, when generating random numbers, storage space is saved and storage overhead is reduced.
[0047] Combined with Figure 1 As shown, embodiments of the present disclosure provide an application scenario applicable to a method for generating random numbers. This application scenario includes a terminal device 11 and an electronic device 12. A network connection is established between the terminal device 11 and the electronic device 12.
[0048] In embodiments of the present disclosure, the terminal device 11 can send a random number generation request to the electronic device 12 according to the user's needs, and receive the random number fed back by the electronic device 12 and display it to the user. For example, the terminal device 11 can be a mobile phone, a computer, a smart interactive tablet, etc.
[0049] In embodiments of the present disclosure, the electronic device 12 can be a computer with a random number generation function. The electronic device 12 can generate a random number according to the random number generation request sent by the terminal device 11 and feed it back to the terminal device 11. The user can also directly input a random number generation instruction to the electronic device 12, so that the electronic device 12 directly generates a random number according to the random number generation instruction. For example, the electronic device 12 can be a mobile phone, a computer, a smart interactive tablet, etc., the electronic device 12 can also be a random number generator, and the electronic device 12 can also be a server.
[0050] Combined with Figure 2 As shown, embodiments of the present disclosure provide a method for generating random numbers. This method can be applied to the electronic device in the above embodiments. The method includes:
[0051] S21, determine a first storage length and a second storage length according to the storage length of the random number.
[0052] S22. Generate a first set of numbers according to the first storage length; and generate a second set of numbers according to the second storage length.
[0053] S23. Select the numbers in the first set of numbers in sequence, and combine them with all the numbers in the second set of numbers respectively to obtain random numbers.
[0054] Among them, the storage length of the random number is equal to the sum of the first storage length and the second storage length. The first storage length is the storage length of each number in the first set of numbers, and the second storage length is the storage length of each number in the second set of numbers.
[0055] Since the working principle of a computer is to perform operations using the binary algorithm generated by high and low levels, in the method for generating random numbers provided in the embodiments of the present disclosure, for the convenience of operation, the random numbers are in binary format. Specifically, when generating random numbers, the storage length of the random number to be generated can be divided into a first storage length and a second storage length, and the sum of the first storage length and the second storage length is the storage length of the binary random number. According to the first storage length, a first set of numbers can be generated, and the storage length of each number in the first set of numbers is the first storage length. And according to the second storage length, a second set of numbers can be generated, and the storage length of each number in the second set of numbers is the second storage length. Select the numbers in the first set of numbers in sequence, and combine them with all the numbers in the second set of numbers respectively to obtain random numbers. In the embodiments of the present disclosure, since the storage length of the random number to be generated is the sum of the first storage length and the second storage length, then the numbers with the first storage length in the first set of numbers and the numbers with the second storage length in the second set of numbers can be combined to generate random numbers with a storage length consistent with the storage length of the random number to be generated, so that the generated random numbers meet the user's requirements. And, after selecting the numbers in the first set of numbers in sequence and combining them with all the numbers in the second set of numbers respectively, the combination method obtained is unique and non-repetitive, which can ensure that the combined random numbers are also non-repetitive. In this way, in the embodiments of the present disclosure, during the process of generating random numbers, it is not necessary to perform duplicate verification on the generated random numbers. Therefore, when the range of random numbers required by the user is large, the generation efficiency of random numbers can be improved.
[0056] In addition, when the embodiments of the present disclosure generate random numbers, only the first set of numbers and the second set of numbers need to be stored. Since the newly generated random numbers do not need to be subjected to duplicate verification, it is not necessary to store the random numbers that have been used by the user within the range of random number values. Therefore, when generating random numbers, the storage space is saved and the storage overhead is reduced.
[0057] Optionally, before the above step S21, the process by which the electronic device determines the storage length of the random number to be generated may be as follows: Obtain a generation request for the random number to be generated; wherein, the generation request for the random number to be generated carries the value range of the random number to be generated. Determine the storage length of the random number to be generated according to the value range of the random number to be generated.
[0058] In this embodiment, the value range of the random number to be generated is provided by the user. The user can send a generation request carrying the value range of the random number to be generated to the electronic device through the terminal device. Since the value range of the random number to be generated provided by the user is usually in decimal format, the electronic device needs to determine the storage length of the binary random number to be generated according to the value range of the decimal random number to be generated.
[0059] Exemplarily, assume that the value range of the random number required by the user is [0 to 16], then the storage length of the corresponding binary random number is 4 bits. In this example, the value range corresponding to the binary data with a storage length of 4 bits exactly corresponds to the value range of the decimal data [0 to 16]. However, the value range corresponding to the binary data with a storage length of 3 bits corresponds to the value range of the decimal data [0 to 7]. When the value range of the random number required by the user is [0 to 12], since the value range of the decimal data corresponding to the 3-bit storage length [0 to 7] cannot meet the requirement, the electronic device can determine that the storage length of the random number is 4 bits. In this way, the storage length of the random number to be generated determined by the electronic device needs to be greater than or equal to the data storage length corresponding to the value range of the random number required by the user to ensure that the random number generated by the electronic device meets the user's requirements.
[0060] In addition, in this embodiment, when the storage length of the random number to be generated determined by the electronic device is greater than the data storage length corresponding to the value range of the random number required by the user, there may be invalid random numbers in the random numbers determined by the electronic device according to the storage length of the random number to be generated, and the invalid random numbers need to be filtered out. Combining the previous example, assume that the value range of the random number required by the user is [0 to 12], that is, the storage length of the random number to be generated determined by the electronic device is 4 bits. Since the value range of the random numbers corresponding to the 4-bit storage length is [0 to 16], the electronic device can filter out the invalid random numbers 13, 14, 15, and 16 in the value range [0 to 16]. Thus, it is further ensured that the random number generated by the electronic device meets the user's requirements.
[0061] Further, in the above step S22, to generate the first set of numbers according to the first storage length, it includes: determining the first value range according to the first storage length; generating the first set of numbers based on the first value range; and generating the second set of numbers according to the second storage length, including: determining the second value range according to the second storage length; generating the second set of numbers based on the second value range. Wherein, the first set of numbers includes all the numbers within the first value range, and the second set of numbers includes all the numbers within the second value range.
[0062] Exemplarily, assume that the first storage length is 3 digits, then the corresponding first value range in decimal format is [0 to 7]. The binary values corresponding to the first value range [0 to 7] include 0 0 0, 0 0 1, 0 1 0, 1 0 0, 0 1 1, 1 0 1, 1 1 0, and 1 1 1. The first set of numbers corresponding to the first value range [0 to 7] can be {0, 3, 6, 7, 5, 4, 2, 1}. Assume that the second storage length is 2 digits, then the corresponding second value range in decimal format is [0 to 3]. The binary values corresponding to the second value range [0 to 3] include 0 0, 0 1, 1 0, and 1 1. The first set of numbers corresponding to the second value range [0 to 3] can be {0, 3, 1, 2}. It is worth mentioning that the first set of numbers needs to include all the numbers within the first value range, and the second set of numbers also needs to include all the numbers within the second value range. In this way, it can ensure that the random numbers generated subsequently based on the first set of numbers and the second set of numbers are more comprehensive and avoid the situation of missing the generation of random numbers.
[0063] Optionally, as shown in Figure 3 The exemplary process of obtaining random numbers by successively selecting the numbers in the first set of numbers and combining them with all the numbers in the second set of numbers in the above step S23 can be as follows: Assume that the storage length of the random number is L, the first storage length is M, the second storage length is N, the first set of numbers is A, the second set of numbers is B, and the number combination is C. Wherein, A1 is the first number in the first set of numbers, A2 is the second number in the first set of numbers... A i is the i-th number in the first set of numbers. i is the number of numbers in the first set of numbers A, and i = 2 M . B1 is the first number in the second set of numbers, B2 is the second number in the second set of numbers... B j is the j-th number in the second set of numbers. j is the number of numbers in the second set of numbers B, and j = 2 N . S is the number of random numbers, and S = M * N.
[0064] In this embodiment, when combining the numbers in the first number set A and the numbers in the second number set B, first select the number A1 in the first number set A and combine it with all the numbers B1, B2, ···, B in the second number set j respectively. Then select the number A2 in the second number set A and combine it with all the numbers B1, B2, ···, B in the second number set j respectively, and so on until all the numbers in the first number set A are selected. Thus, S random numbers are obtained. Each random number C is formed by combining two numbers, and the two numbers belong to the first number set A and the second number set B respectively, and the sum of the storage lengths of the two numbers is the storage length L of the random number.
[0065] Optionally, in the above step S23, considering that in the field of information security technology, the generated random numbers can also be used as verification codes for security verification. To improve the security of the random numbers, in this embodiment, the numbers in the first number set can be combined with the numbers in the second number set in an encrypted manner, specifically including: when the storage length of the first number is greater than the storage length of the second number, extend the storage length of the second number to the first storage length. Calculate the exclusive OR operation result between the first number and the extended second number. Combine the exclusive OR operation result with the second number to obtain a random number. Wherein, the first number is a number sequentially selected from the first number set, and the second number is any number in the second number set.
[0066] Exemplarily, as shown in Figure 4 the storage length of the first number A1 can be 8 bits, the storage length of the second number B1 can be 4 bits, and the values of each bit of the first number A1 are a1, a2, a3, a4, a5, a6, a7, a8 respectively, and the values of each bit of the second number B1 are b1, b2, b3, b4 respectively. The process of encrypting and combining the first number A1 and the second number B1 can be as follows: First, expand the second number B1. The expansion of B1 can adopt the self-loop expansion method. After expanding B1 to 8 bits, the values of each bit are b1, b2, b3, b4, b1, b2, b3, b4 respectively. Then, perform a bitwise exclusive OR operation on A1 and the expanded B1, and the values of each bit of the obtained exclusive OR operation result are Finally, after combining the exclusive OR operation result with the second number B1, the values of each bit of the 12-bit random number obtained can be b1, b2, b3, b4. In this way, by using this method to encrypt and combine the numbers in the first number set with the numbers in the second number set, the combination rule of the numbers is not easily cracked. Thus, the security of the generated random numbers is improved. In addition, in the implementation of the present disclosure, the method of encrypting and combining the first number A1 in the first number set with the number B1 in the second number set is not limited to extending the storage length of B1 and performing an exclusive OR operation based on A1 and B1, and combining the exclusive OR operation result with B1. As long as each digit value in the first number A1 and each digit value in the exclusive OR operation result conform to the surjective rule, the present disclosure embodiments do not make specific limitations on this.
[0067] Optionally, in the above step S23, when the storage length of the first number is equal to the storage length of the second number, the numbers in the first number set are sequentially selected and combined with all the numbers in the second number set respectively to obtain random numbers, including: calculating the exclusive OR operation result between the first number and the second number. Combining the exclusive OR operation result with the first number or the second number to obtain a random number. Wherein, the first number is a number sequentially selected from the first number set, and the second number is any number in the second number set.
[0068] In this implementation manner, when the storage length of the first number is equal to the storage length of the second number, a mapping relationship is satisfied between the first number set and the second number set, that is, for each number in the first number set, there is a unique corresponding number in the second number set. And, the storage length of the first number being equal to the storage length of the second number indicates that the storage lengths of the numbers in the first number set are the same as the storage lengths of the numbers in the second number set. This enables the exclusive OR operation between the first number and the second number without the need for extension. Moreover, since a mapping relationship is satisfied between the first number set and the second number set and the storage length of the first number is equal to the storage length of the second number, the exclusive OR operation result between the first number and the second number can be combined with the first number or the second number to obtain a random number.
[0069] For example, the first number is 0 1 0 1 with 4 bits, the second number is 1 1 0 0 with 4 bits, the exclusive OR operation result of the first number and the second number is 1 0 0 1. The exclusive OR operation result can be combined with the first number to obtain an 8-bit random number 0 1 0 1 10 0 1, and the exclusive OR operation result can also be combined with the second number to obtain an 8-bit random number 1 0 0 1 1 1 0 0. In this way, when the storage length of the first number is equal to the storage length of the second number, the method of encrypting and combining the first number and the second number is more flexible, which can increase the complexity of the combination of numbers. Thus, the security of the generated random numbers is further improved.
[0070] Optionally, considering that the random numbers usually required by users are in decimal format, and the random numbers generated by the electronic device are in binary format. Compared with decimal format data, binary format data appears to be more verbose in terms of user perception and usage. If the binary format random numbers are directly fed back to the user, it is not convenient for the user to use. After generating the random numbers in step S23 above, the random numbers can be converted to decimal format. Combining with the previous example, if the random numbers generated by the electronic device are 1 0 0 1 1 1 0 0, the random numbers converted to decimal format are 156. Additionally, if the user needs multiple random numbers simultaneously, multiple decimal random numbers can be concatenated into a random number sequence to enhance the user experience of using random numbers.
[0071] Combined with Figure 5 As shown, for a clearer illustration or for facilitating the implementation of the solutions of this application, the embodiments of the present disclosure provide another method for generating random numbers. This method is exemplarily described with the storage length of the random numbers to be generated being 4 bits, the first storage length being 3 bits, and the second storage length being 1 bit. Specifically as follows:
[0072] S51, the first digital set A corresponding to the first storage length can be {110, 001, 010, 100, 101, 111, 011, 000}.
[0073] S52, the second digital set B corresponding to the second storage length can be {1, 0}.
[0074] S53, after expanding the storage lengths of each digit in the second digital set B, the expanded second digital set B {111, 000} is obtained.
[0075] S54, sequentially select the digits in the first digital set and combine them with all the digits in the second digital set B respectively. The obtained digital combinations include (110, 111), (110, 000), (001, 111), (001, 000), (010, 111), (010, 000), (100, 111), (100, 000), (101, 111), (101, 000), (111, 111), (111, 000), (011, 111), (011, 000), (000, 111), (000, 000).
[0076] S55, perform exclusive OR operations on the two digits in each digital combination respectively. The obtained exclusive OR operation results include (001), (110), (110), (001), (101), (010), (011), (100), (010), (101), (000), (111), (100), (011), (111), (000).
[0077] S56. After combining the XOR results in each digital combination with the numbers before expansion belonging to the second digital set B respectively, random numbers obtained include 0011, 1100, 1101, 0010, 1011, 0100, 0111, 1000, 0101, 1010, 0001, 1110, 1001, 0110, 1111, 0000.
[0078] S57. After converting each random number into a random number in decimal format respectively, a decimal random number sequence obtained is 3, 12, 13, 2, 11, 4, 7, 8, 5, 10, 1, 14, 9, 6, 15, 0.
[0079] By using the method for generating random numbers provided by the embodiments of the present disclosure, a decimal random sequence can be obtained, and each random number in the random sequence does not have a duplicate situation. In this way, in the embodiments of the present disclosure, during the process of generating random numbers, it is not necessary to perform duplicate verification on the generated random numbers. Therefore, when the range of random numbers required by users is relatively large, the generation efficiency of random numbers can be improved.
[0080] In addition, when the embodiments of the present disclosure generate random numbers, only the first digital set and the second digital set need to be stored. That is, the first digital set A includes 8 numbers, and each number occupies 3 bits, so it is obtained that the first digital set totally occupies 24 bits. The second digital set B includes 2 numbers, and each number occupies 1 bit, so it is obtained that the second digital set totally occupies 2 bits. The first digital set A and the second digital set B totally occupy 26 bits. However, in the related art, when performing duplicate removal verification on newly generated 4-bit random numbers based on a database, the complete random number sequence may need to be stored in the database. The complete random number sequence includes 16 numbers, and each number occupies 4 bits, so it is obtained that the complete random number sequence totally occupies 64 bits. Obviously, when the embodiments of the present disclosure generate random numbers, only the 26-bit first digital set and second digital set need to be stored, and there is no need to store the complete digital sequence. Therefore, when generating random numbers, the storage space is saved and the storage overhead is reduced.
[0081] Combined with Figure 6 As shown, for the purpose of more clearly explaining or facilitating the implementation of the solution of the present application, the embodiments of the present disclosure provide another method for generating random numbers. This method takes the storage length of the to-be-generated random number as 4 bits, the first storage length as 2 bits, and the second storage length as 2 bits for exemplary illustration. Specifically as follows:
[0082] S61. The first digital set A corresponding to the first storage length can be {11, 00, 01, 10}.
[0083] S62. The second digital set B corresponding to the second storage length can be {10, 11, 00, 01}.
[0084] S63, sequentially select the numbers in the first set of numbers, and combine them with all the numbers in the second set of numbers B respectively. The resulting number combinations include (11, 10), (11, 11), (11, 00), (11, 01), (00, 10), (00, 11), (00, 00), (00, 01), (01, 10), (01, 11), (01, 00), (01, 01), (10, 10), (10, 11), (10, 00), (10, 01).
[0085] S64, perform an exclusive OR operation on the two numbers in each number combination respectively. The resulting exclusive OR operation results include (01), (00), (11), (10), (10), (11), (00), (01), (11), (10), (01), (00), (00), (01), (10), (11).
[0086] S651, after combining the exclusive OR results in each number combination with the numbers belonging to the second set of numbers B respectively, the first set of random numbers obtained includes 0110, 0011, 1100, 1001, 1010, 1111, 0000, 0101, 1110, 1011, 0100, 0001, 0010, 0111, 1000, 1101.
[0087] S661, after converting each random number into a random number in decimal format respectively, the first sequence of random numbers R1 in decimal format is 6, 3, 12, 9, 10, 15, 0, 5, 14, 11, 4, 1, 2, 7, 8, 13.
[0088] S652, after combining the exclusive OR results in each number combination with the numbers belonging to the first set of numbers B respectively, the second set of random numbers obtained includes 1101, 0000, 0111, 1010, 1110, 0011, 0100, 1001, 1111, 0010, 0101, 1000, 1100, 0001, 0110, 1011.
[0089] S662, after converting each random number into a random number in decimal format respectively, the second sequence of random numbers R2 in decimal format is 13, 0, 7, 10, 14, 3, 4, 9, 15, 2, 5, 8, 12, 1, 6, 11.
[0090] By using the method for generating random numbers provided in the embodiments of the present disclosure, a first random sequence or a second random sequence in decimal can be obtained, and each random number in the random sequence does not have a duplicate situation. In this way, in the embodiments of the present disclosure, during the process of generating random numbers, it is not necessary to perform duplicate verification on the generated random numbers. Therefore, when the range of random numbers required by the user is large, the generation efficiency of random numbers can be improved.
[0091] In addition, when the embodiments of the present disclosure generate random numbers, only the first digital set and the second digital set need to be stored. That is, the first digital set A includes 4 numbers, and each number occupies 2 bits, so it can be obtained that the first digital set occupies a total of 8 bits. The second digital set B includes 4 numbers, and each number occupies 2 bits, so it can be obtained that the second digital set occupies a total of 8 bits. The first digital set A and the second digital set B together occupy 16 bits. However, in the related art, when performing duplicate verification on the newly generated 4-bit random numbers based on the database, the complete random number sequence may need to be stored in the database. The complete random number sequence includes 16 numbers, and each number occupies 4 bits, so it can be obtained that the complete random number sequence occupies a total of 64 bits. Obviously, when the embodiments of the present disclosure generate random numbers, only the 16-bit first digital set and second digital set need to be stored, and there is no need to store the complete digital sequence. Therefore, when generating random numbers, the storage space is saved and the storage overhead is reduced.
[0092] In addition, combined with Figure 5 and Figure 6 the corresponding methods for generating random numbers respectively, it is not difficult to obtain that when generating random numbers, the closer the first storage length and the second storage length are, the smaller the occupied storage space. For example, when the storage length of the random numbers to be generated is 4 bits, the division method with the first storage length of 3 bits and the second storage length of 1 bit occupies a total of 26 bits of storage space. The division method with the first storage length of 2 and the second storage length of 2 occupies a total of 16 bits of storage space. Obviously, the closer the first storage length and the second storage length are, the smaller the occupied storage space.
[0093] It should be noted that setting the storage length of the random numbers to be generated as 4 bits is only for facilitating the example to illustrate the solution of the present application. It can be imagined that in the method for generating random numbers provided in the embodiments of the present disclosure, the closer the divided first storage length and the second storage length are, the higher the complexity of the encryption combination method of the first number and the second number, the more difficult it is to crack the process of generating random numbers, and the higher the security of the random numbers.
[0094] Combined with Figure 7 as shown, the embodiments of the present disclosure provide a device for generating random numbers. This device can be integrated into the electronic device in the above embodiments. The device includes a determination module 701, a generation module 702, and a combination module 703, where:
[0095] A determination module 701, configured to determine a first storage length and a second storage length according to the storage length of a random number to be generated.
[0096] A generation module 702, configured to generate a first set of numbers according to the first storage length; and generate a second set of numbers according to the second storage length.
[0097] A combination module 703, configured to sequentially select numbers from the first set of numbers and respectively combine them with all the numbers in the second set of numbers to obtain a random number.
[0098] Wherein, the storage length of the random number is equal to the sum of the first storage length and the second storage length; the first storage length is the storage length of each number in the first set of numbers, and the second storage length is the storage length of each number in the second set of numbers.
[0099] Further, the generation module 702 is further configured to: determine a first value range according to the first storage length; generate a first set of numbers based on the first value range; wherein, the first set of numbers includes all the numbers within the first value range.
[0100] Further, the generation module 702 is further configured to: determine a second value range according to the second storage length; generate a second set of numbers based on the second value range; wherein, the second set of numbers includes all the numbers within the second value range.
[0101] Further, the combination module 703 is further configured to: when the storage length of the first number is greater than the storage length of the second number, extend the storage length of the second number to the first storage length; calculate the XOR operation result between the first number and the extended second number; combine the XOR operation result with the second number to obtain a random number; wherein, the first number is a number sequentially selected from the first set of numbers, and the second number is any number in the second set of numbers.
[0102] Further, the combination module 703 is further configured to: when the storage length of the first number is equal to the storage length of the second number, calculate the XOR operation result between the first number and the second number; combine the XOR operation result with the first number or the second number to obtain a random number; wherein, the first number is a number sequentially selected from the first set of numbers, and the second number is any number in the second set of numbers.
[0103] Optionally, the determination module 701 is further configured to: obtain a generation request for a random number to be generated; wherein, the generation request for the random number to be generated carries the value range of the random number to be generated; determine the storage length of the random number to be generated according to the value range of the random number to be generated.
[0104] Optionally, the combination module 703 is further configured to: convert the random number into a decimal format to form a random sequence including a plurality of decimal random numbers.
[0105] The method for generating a random number provided by the embodiments of the present disclosure can perform the actions of the electronic device in the above embodiments. The implementation principle and technical effects are similar, and will not be elaborated here.
[0106] Combined Figure 8 As shown in the figure, an electronic device 800 provided by the embodiments of the present disclosure includes a processor 100 and a memory 101. Optionally, the electronic device 800 may further include a communication interface 102 and a bus 103. Among them, the processor 100, the communication interface 102, and the memory 101 can complete mutual communication through the bus 103. The communication interface 102 can be used for information transmission. The processor 100 can call the logical instructions in the memory 101 to execute the method for generating a random number in the above embodiments.
[0107] In addition, when the logical instructions in the above-mentioned memory 101 are implemented in the form of a software functional unit and sold or used as an independent product, they can be stored in a computer-readable storage medium.
[0108] The memory 101, as a computer-readable storage medium, can be used to store software programs and computer-executable programs, such as the program instructions / modules corresponding to the methods in the embodiments of the present disclosure. The processor 100 executes functional applications and data processing by running the program instructions / modules stored in the memory 101, that is, implements the method for generating a random number in the above embodiments.
[0109] The memory 101 may include a program storage area and a data storage area. Among them, the program storage area may store an operating system and application programs required for at least one function; the data storage area may store data created according to the use of the terminal device, etc. In addition, the memory 101 may include a high-speed random access memory and may also include a non-volatile memory.
[0110] The embodiments of the present disclosure provide a storage medium storing computer-executable instructions, and the computer-executable instructions are set to execute the above method for generating a random number.
[0111] The above storage medium may be a transient computer-readable storage medium or a non-transient computer-readable storage medium.
[0112] The technical solution of the embodiments of the present disclosure may be embodied in the form of a software product, which is stored in a storage medium and includes one or more instructions for causing a computer device (which may be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the method described in the embodiments of the present disclosure. The foregoing storage medium may be a non-transitory storage medium, including: various media that can store program codes such as USB flash drives, mobile hard disks, read-only memories (ROMs), random access memories (RAMs), magnetic disks, or optical discs, or may also be a transient storage medium.
[0113] The above description and the drawings fully illustrate the embodiments of the present disclosure so that those skilled in the art can practice them. Other embodiments may include structural, logical, electrical, process, and other changes. Embodiments only represent possible variations. Unless explicitly required, separate components and functions are optional, and the order of operations may vary. Parts and features of some embodiments may be included in or substituted for parts and features of other embodiments. Moreover, the terms used in this application are only for describing embodiments and are not used to limit the claims. As used in the description of the embodiments and the claims, unless the context clearly indicates otherwise, the singular forms "a", "an", and "the" are intended to also include the plural forms. Similarly, as used in this application, the term "and / or" refers to any and all possible combinations including one or more of the associated listed items. Additionally, when used in this application, the term "comprise" and its variants "comprises" and / or "comprising" etc. mean the presence of the stated features, wholes, steps, operations, elements, and / or components, but do not exclude the presence or addition of one or more other features, wholes, steps, operations, elements, components, and / or groups of these. Without further limitation, an element defined by the statement "comprising one..." does not exclude the presence of additional identical elements in the process, method, or device including the element. In this document, each embodiment may focus on the differences from other embodiments, and the same or similar parts among the embodiments may be referred to each other. For the methods, products, etc. disclosed in the embodiments, if they correspond to the method part disclosed in the embodiments, the relevant parts may refer to the description of the method part.
[0114] Those skilled in the art can realize that the units and algorithm steps of each example described in combination with the embodiments disclosed herein can be implemented by electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are executed in a hardware or software manner may depend on the specific application and design constraints of the technical solution. The skilled person can use different methods for each specific application to implement the described functions, but such implementation should not be considered to exceed the scope of the embodiments of the present disclosure. The skilled person can clearly understand that for the convenience and conciseness of description, the specific working processes of the systems, devices, and units described above can refer to the corresponding processes in the foregoing method embodiments and will not be elaborated herein.
[0115] In the embodiments disclosed herein, the disclosed methods, products (including but not limited to devices, equipment, etc.) can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of the units can be merely a logical function division, and there can be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. In addition, the couplings or direct couplings or communication connections shown or discussed with each other can be through some interfaces, and the indirect couplings or communication connections of the devices or units can be in electrical, mechanical, or other forms. The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they can be located in one place or distributed to multiple network units. Some or all of the units can be selected according to actual needs to implement this embodiment. In addition, the functional units in the embodiments of the present disclosure can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit.
[0116] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to embodiments of the present disclosure. In this regard, each block in the flowchart or block diagram may represent a module, a segment of code, or a part thereof, which contains one or more executable instructions for implementing the specified logical function. In some alternative implementations, the functions noted in the blocks may occur in a different order than that noted in the accompanying drawings. For example, two consecutive blocks may actually be executed substantially in parallel, or they may sometimes be executed in the reverse order, depending on the functions involved. In the descriptions corresponding to the flowcharts and block diagrams in the accompanying drawings, the operations or steps corresponding to different blocks may also occur in a different order than that disclosed in the description, and sometimes there is no specific order between different operations or steps. For example, two consecutive operations or steps may actually be executed substantially in parallel, or they may sometimes be executed in the reverse order, depending on the functions involved. Each block in the block diagram and / or flowchart, and combinations of blocks in the block diagram and / or flowchart, can be implemented by a dedicated hardware-based system that performs the specified functions or actions, or can be implemented by a combination of dedicated hardware and computer instructions.
Claims
1. A method for generating random numbers, characterized in that, Including: Determine a first storage length and a second storage length according to the storage length of the random number to be generated; Generate a first set of numbers according to the first storage length; And generate a second set of numbers according to the second storage length; Select the numbers in the first set of numbers in sequence, and combine them with all the numbers in the second set of numbers respectively to obtain a random number; Wherein, the storage length of the random number is equal to the sum of the first storage length and the second storage length; The first storage length is the storage length of each number in the first set of numbers, and the second storage length is the storage length of each number in the second set of numbers.
2. The method according to claim 1, characterized in that Generate a first set of numbers according to the first storage length, including: Determine a first value range according to the first storage length; Generate a first set of numbers based on the first value range; Wherein, the first set of numbers includes all the numbers within the first value range.
3. The method according to claim 1, wherein Generate a second set of numbers according to the second storage length, including: Determine a second value range according to the second storage length; Generate a second set of numbers based on the second value range; Wherein, the second set of numbers includes all the numbers within the second value range.
4. The method according to claim 1, wherein Select the numbers in the first set of numbers in sequence, and combine them with all the numbers in the second set of numbers respectively to obtain a random number, including: When the storage length of the first number is greater than the storage length of the second number, extend the storage length of the second number to the first storage length; Calculate the exclusive OR operation result between the first number and the extended second number; Combine the exclusive OR operation result with the second number to obtain a random number; Wherein, the first number is a number selected from the first set of numbers in sequence, and the second number is any number in the second set of numbers.
5. The method according to claim 1, characterized in that Select the numbers in the first set of numbers in sequence, and combine them with all the numbers in the second set of numbers respectively to obtain a random number, including: When the storage length of the first number is equal to the storage length of the second number, calculate the exclusive OR operation result between the first number and the second number; Combine the exclusive OR operation result with the first number or the second number to obtain a random number; Wherein, the first number is a number selected from the first set of numbers in sequence, and the second number is any number in the second set of numbers.
6. The method according to any one of claims 1 to 5, characterized in that The method further includes: Obtain a generation request for the random number to be generated; wherein, the generation request for the random number to be generated carries the value range of the random number to be generated; Determine the storage length of the random number to be generated according to the value range of the random number to be generated.
7. The method according to any one of claims 1 to 5, characterized in that After obtaining the random number, the method further includes: Convert the random number into a decimal format to form a random sequence including multiple decimal random numbers.
8. A device for generating random numbers, characterized in that, Including: A determination module, configured to determine a first storage length and a second storage length according to the storage length of the random number to be generated; A generation module, configured to generate a first set of numbers according to the first storage length; And generate a second set of numbers according to the second storage length; A combination module, configured to select the numbers in the first set of numbers in sequence, and combine them with all the numbers in the second set of numbers respectively to obtain a random number; Wherein, the storage length of the random number is equal to the sum of the first storage length and the second storage length; The first storage length is the storage length of each digit in the first set of digits, and the second storage length is the storage length of each digit in the second set of digits.
9. An electronic device, comprising a processor and a memory storing program instructions, characterized in that, The processor is configured to execute the method for generating a random number according to any one of claims 1 to 7 when running the program instructions.
10. A storage medium stores program instructions, characterized in that, When the program instructions are running, the method for generating a random number according to any one of claims 1 to 7 is executed.