CN113778380A - Method for rapidly generating non-repeated random natural numbers - Google Patents

Method for rapidly generating non-repeated random natural numbers Download PDF

Info

Publication number
CN113778380A
CN113778380A CN202110680934.4A CN202110680934A CN113778380A CN 113778380 A CN113778380 A CN 113778380A CN 202110680934 A CN202110680934 A CN 202110680934A CN 113778380 A CN113778380 A CN 113778380A
Authority
CN
China
Prior art keywords
numbers
random
computational complexity
generating
algorithm
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202110680934.4A
Other languages
Chinese (zh)
Inventor
王璞
张亮
高舜龙
宋兴瑞
罗英
季尧遥
张海丰
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Nanjing Guoke Software Co ltd
Original Assignee
Nanjing Guoke Software Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by Nanjing Guoke Software Co ltd filed Critical Nanjing Guoke Software Co ltd
Priority to CN202110680934.4A priority Critical patent/CN113778380A/en
Publication of CN113778380A publication Critical patent/CN113778380A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F7/00Methods or arrangements for processing data by operating upon the order or content of the data handled
    • G06F7/58Random or pseudo-random number generators

Landscapes

  • Physics & Mathematics (AREA)
  • General Physics & Mathematics (AREA)
  • Engineering & Computer Science (AREA)
  • Theoretical Computer Science (AREA)
  • Computational Mathematics (AREA)
  • Mathematical Analysis (AREA)
  • Mathematical Optimization (AREA)
  • Pure & Applied Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • Management, Administration, Business Operations System, And Electronic Commerce (AREA)
  • Complex Calculations (AREA)

Abstract

The invention discloses a method for quickly generating non-repeated random natural numbers, which comprises defining two sets S and T with empty initial values, defining a one-dimensional array F with the length of M, and initializing the numerical value of each element in F to 1, namely F [1: M%]1. Circularly calling the random generation function for N times, wherein the omega region of the calling random generation function for the k time is omegak=[1,M‑k+1]Where k is 1,2, …, N, each randomly generating a number SkAnd stored in S in order. Sequentially cycling each element S in the set Sk(k is 1,2, …, N), calculated to satisfy
Figure DEST_PATH_FDA0003323284220000011
Add the index J to the set T and let F [ J ]]0. The set T is returned. The algorithm solves the problem that a part of unrepeated natural number sequence can be rapidly selected in a given natural number region.

Description

Method for rapidly generating non-repeated random natural numbers
Technical Field
The invention relates to the technical field of natural number generation, in particular to a quick generation method of non-repetitive random natural numbers.
Background
At present, computer numerical simulation has incomparable importance in various fields such as meteorological forecasting, oil reservoir exploration, material mechanics, logistics traffic, biological medicine and the like. The generation of non-repeated random natural numbers plays an important role in computer numerical simulation, for example, when weather conditions of cities in the country at a certain moment are simulated, 30% of the cities in the country are required to be cloudy, and the cities are uniformly distributed on a map with equal probability; for another example, for a second-generation gene sequencer chip, the chip has 1300 ten thousand pores, and it is required that microspheres exist in 85% of the pores, and the positions of the pores where the microspheres exist on the chip have equal probability uniform distribution. The specific algorithm is set forth as follows:
given that the natural number region is Ω ═ 1, M ], it is required to randomly generate N non-repeating numbers from Ω. It should be noted that when N > M/2, the problem is equivalent to generating all the numbers in Ω, and then removing M-N non-repeating numbers from the generated numbers. That is, only the case where N non-repeating numbers are randomly generated from Ω and N ≦ M/2 needs to be considered.
The traditional calculation flow for generating non-repetitive natural numbers is as follows:
the first step is as follows: a set S for storing non-repeating words is defined and assigned null.
The second step is that: a number alpha is randomly generated within omega.
The third step: judging whether alpha is in S, if so, performing the second step again; if not, α is added to S.
The fourth step: judging whether the number of S is not more than N, if so, performing a second step; if not, exiting.
The calculation process cannot solve the problem that a part of unrepeated natural number sequences can be rapidly selected in a given natural number area.
To this end, we propose a fast generation method of non-repetitive random natural numbers to solve the above problems.
Disclosure of Invention
The invention aims to solve the defects in the prior art, and provides a quick generation method of random numbers without repetition, which can not quickly select a part of unrepeated natural number sequences in a given natural number region.
In order to achieve the purpose, the invention adopts the following technical scheme: a method for rapidly generating random numbers without repetition is characterized by comprising the following steps:
s1: defining two sets S and T with empty initial values, defining a one-dimensional array F with length M, and initializing the value of each element in F to 1, i.e., F [1: M ] ═ 1.
S2: circularly calling the random generation function for N times, wherein the omega region of the calling random generation function for the k time is omegak=[1,M-k+1]Where k is 1,2, N, each randomly generating a number SkAnd stored in S in order.
S3: sequentially cycling each element S in the set Sk(k. 1, 2. N), calculated to satisfy
Figure RE-GDA0003323284230000021
Add the index J to the set T and let F [ J ]]=0。
S4: the set T is returned.
Preferably, each element in the set T is a random natural number that does not repeat and satisfies a condition.
Preferably, assuming that the computational complexity of generating a random number is p, the computational complexity of comparing the two numbers is q, and the computational complexity of the addition and subtraction operation is r, the computational complexity of the algorithm of the present design can be analyzed as follows: the amount calculated in the second step is D2Np + (N-1) r, the amount calculated in the third step is
Figure RE-GDA0003323284230000022
Preferably, the method for rapidly generating random numbers without repeating according to claim 3, wherein the calculation amount of the algorithm is:
Figure RE-GDA0003323284230000031
preferably, the above-mentioned calculation amount of the algorithm is calculated by substituting: if remember
Figure RE-GDA0003323284230000032
Then
Figure RE-GDA0003323284230000033
Since the computational complexity of comparing two numbers is equivalent to that of addition and subtraction, it can be assumed that r is q, and then Δ can be simplified as:
Figure RE-GDA0003323284230000034
preferably, the computational complexity for generating the random number p ═ O (M)2) The computational complexity q ═ o (m) of the two numbers magnitudes is compared, so the magnitude of expression Δ is:
Figure RE-GDA0003323284230000035
compared with the prior art, the invention has the beneficial effects that:
1. the invention provides a method for quickly generating non-repeated random natural numbers, which comprises the steps of defining a set S and a set T with two initial values of null, defining a one-dimensional array F with the length of M, and initializing the numerical value of each element in the F to be 1, namely F [1: M]1. Circularly calling the random generation function for N times, wherein the omega region of the calling random generation function for the k time is omegak=[1,M-k+1]Whereink 1,2, N, each randomly generating a number SkAnd stored in S in order. Sequentially cycling each element S in the set Sk(k. 1, 2. N), calculated to satisfy
Figure RE-GDA0003323284230000036
Add the index J to the set T and let F [ J ]]0. The set T is returned.
2. In the method for rapidly generating the random natural number without repetition, each element in a set T is the random natural number without repetition which meets the condition, the calculation complexity of generating one random number is assumed to be p, the calculation complexity of comparing the sizes of two numbers is assumed to be q, and the calculation complexity of addition and subtraction operation is assumed to be r, so the calculated amount of the algorithm of the design can be analyzed as follows: the amount calculated in the second step is D2Np + (N-1) r, the amount calculated in the third step is
Figure RE-GDA0003323284230000041
The calculation amount of the algorithm is as follows:
Figure RE-GDA0003323284230000042
the calculation amount of the algorithm is obtained by substituting calculation: if remember
Figure RE-GDA0003323284230000043
Then
Figure RE-GDA0003323284230000044
Since the computational complexity of comparing two numbers is equivalent to that of addition and subtraction, it can be assumed that r is q, and then Δ can be simplified as:
Figure RE-GDA0003323284230000045
the computational complexity due to the generation of random numbers p ═ O (M)2) Comparing the sizes of two numbersThe computational complexity q ═ o (m), so the expression Δ is of the order of magnitude
Figure RE-GDA0003323284230000046
Drawings
Fig. 1 is a step diagram of a method for rapidly generating random numbers without repetition according to the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments.
Example 1
A method for rapidly generating random numbers without repetition comprises the following steps:
s1: defining two sets S and T with empty initial values, defining a one-dimensional array F with length M, and initializing the value of each element in F to 1, i.e., F [1: M ] ═ 1.
S2: circularly calling the random generation function for N times, wherein the omega region of the calling random generation function for the k time is omegak=[1,M-k+1]Where k is 1,2, N, each randomly generating a number SkAnd stored in S in order.
S3: sequentially cycling each element S in the set Sk(k. 1, 2. N), calculated to satisfy
Figure RE-GDA0003323284230000051
Add the index J to the set T and let F [ J ]]=0。
S4: the set T is returned.
Example 2
Each element in the set T is a random natural number that does not repeat and satisfies the condition, and assuming that the computation complexity of generating one random number is p, the computation complexity of comparing the sizes of two numbers is q, and the computation complexity of the addition and subtraction operation is r, the computation amount of the algorithm of the design can be analyzed as follows: the second step is calculated as
D2=Np+(N-1)r,
The third step is calculated by
Figure RE-GDA0003323284230000052
The calculation amount of the algorithm is as follows:
Figure RE-GDA0003323284230000053
the calculation amount of the algorithm is obtained by substituting calculation: if remember
Figure RE-GDA0003323284230000054
Then
Figure RE-GDA0003323284230000055
Since the computational complexity of comparing two numbers is equivalent to that of addition and subtraction, it can be assumed that r is q, and then Δ can be simplified as:
Figure RE-GDA0003323284230000056
the computational complexity due to the generation of random numbers p ═ O (M)2) The computational complexity q ═ o (m) of the two numbers is compared, so that the expression Δ is of the order of magnitude
Figure RE-GDA0003323284230000061
Example 3
Examples are: it is required to randomly generate 10 non-repeating natural numbers in [1,20 ].
The first step is as follows: the definition set S [ ], T [ ], and the one-dimensional array F [1:20] ═ 1.
The second step is that: the random generating function is called 10 times in a circulating way, and the omega region of the random generating function called the k time isΩk=[1,21-k]Where k is 1,2, 10, each one of the randomly generated numbers S is givenkAnd stored in S in order. Suppose that the elements in the S set are S ═ 10,8,13,9,6,3,8,9,7,3, respectively]。
The third step: sequentially cycling each element S in the set Sk(k. 1, 2. 10) calculated to satisfy
Figure RE-GDA0003323284230000062
Add the index J to the set T and let F [ J ]]0. For example to S110, the index J can be found to be 10, satisfying
Figure RE-GDA0003323284230000063
So add the index J to the set T10 and let F [10 ═ 10]0, when T is 10](ii) a To S28, the index J can be found to be 8, satisfying
Figure RE-GDA0003323284230000064
So add the index J-8 to the set T and let F [8 ═ 8]0, when T is [10,8 ]](ii) a To S313, an index J of 15 can be found, satisfying
Figure RE-GDA0003323284230000065
So add the index J15 to the set T and let F [15 ═ 15]0, when T is ═ 10,8,15](ii) a To S4The index J can be found to be 11, satisfying 9
Figure RE-GDA0003323284230000066
So add the index J to the set T of 11 and let F [11 ═ 11]When T is equal to 0, T is equal to [10,8,15, 11 ═ c]. All elements in S are circulated in sequence, and the final T ═ 10,8,15, 11,6,3,13,16,12,4]。
In summary, the following steps: defining two sets S and T with empty initial values, defining a one-dimensional array F with the length of M, and initializing the value of each element in F to 1, namely F [1: M]When the random generation function is called for the k-th time, the region omega of the random generation function is 1k=[1,M-k+1]Wherein k is 1,2, N, each is followed byMachine generated number SkStored in S in sequence, sequentially cycling each element S in the set Sk(k. 1, 2. N), calculated to satisfy
Figure RE-GDA0003323284230000071
Add the index J to the set T and let F [ J ]]Return set T as 0.
Each element in the set T is a random natural number that does not repeat and satisfies the condition, and assuming that the computation complexity of generating one random number is p, the computation complexity of comparing the sizes of two numbers is q, and the computation complexity of the addition and subtraction operation is r, the computation amount of the algorithm of the design can be analyzed as follows: the amount calculated in the second step is D2Np + (N-1) r, the amount calculated in the third step is
Figure RE-GDA0003323284230000072
The calculation amount of the algorithm is as follows:
Figure RE-GDA0003323284230000073
the calculation amount of the algorithm is obtained by substituting calculation: if remember
Figure RE-GDA0003323284230000074
Then
Figure RE-GDA0003323284230000075
Since the computational complexity of comparing two numbers is equivalent to that of addition and subtraction, it can be assumed that r is q, and then Δ can be simplified as:
Figure RE-GDA0003323284230000076
the computational complexity due to the generation of random numbers p ═ O (M)2) The computational complexity q ═ o (m) of the two numbers is compared, so that the expression Δ is of the order of magnitude
Figure RE-GDA0003323284230000077
In the present invention, unless otherwise expressly stated or limited, "above" or "below" a first feature means that the first and second features are in direct contact, or that the first and second features are not in direct contact but are in contact with each other via another feature therebetween. Also, the first feature being "on," "above" and "over" the second feature includes the first feature being directly on and obliquely above the second feature, or merely indicating that the first feature is at a higher level than the second feature. A first feature being "under," "below," and "beneath" a second feature includes the first feature being directly under and obliquely below the second feature, or simply meaning that the first feature is at a lesser elevation than the second feature.
The foregoing shows and describes the general principles, essential features, and advantages of the invention. It will be understood by those skilled in the art that the present invention is not limited to the embodiments described above, and the preferred embodiments of the present invention are described in the above embodiments and the description, and are not intended to limit the present invention. The scope of the invention is defined by the appended claims and equivalents thereof.

Claims (6)

1. A method for rapidly generating random numbers without repetition is characterized by comprising the following steps:
s1: defining two sets S and T with empty initial values, defining a one-dimensional array F with length M, and initializing the value of each element in F to 1, i.e., F [1: M ] ═ 1.
S2: circularly calling the random generation function for N times, wherein the omega region of the calling random generation function for the k time is omegak=[1,M-k+1]Where k is 1,2, N, each randomly generating a number SkAnd stored in S in order.
S3: sequentially cycling each element S in the set Sk(k. 1, 2. N), calculated to satisfy
Figure RE-FDA0003323284220000011
Add the index J to the set T and let F [ J ]]=0。
S4: the set T is returned.
2. The method for rapidly generating random natural numbers without repetition according to claim 1, wherein: each element in the set T is a non-repeating random natural number that satisfies a condition.
3. The method for rapidly generating random natural numbers without repetition according to claim 1, wherein: assuming that the computational complexity of generating a random number is p, the computational complexity of comparing the two numbers is q, and the computational complexity of the addition and subtraction operation is r, the computational complexity of the algorithm of the design can be analyzed as follows: the amount calculated in the second step is D2Np + (N-1) r, the amount calculated in the third step is
Figure RE-FDA0003323284220000012
4. The method for rapidly generating random natural numbers without repetition according to claim 3, wherein the calculation amount of the algorithm is as follows:
Figure RE-FDA0003323284220000013
5. the method as claimed in claim 4, wherein the algorithm calculation is substituted to obtain: if remember
Figure RE-FDA0003323284220000014
Then
Figure RE-FDA0003323284220000015
Since the computational complexity of comparing two numbers is equivalent to that of addition and subtraction, it can be assumed that r is q, and then Δ can be simplified as:
Figure RE-FDA0003323284220000021
6. the method of claim 5, wherein the computational complexity of generating random numbers p ═ O (M) is determined by the method of claim 52) The computational complexity q ═ o (m) of the two numbers magnitudes is compared, so the magnitude of expression Δ is:
Figure RE-FDA0003323284220000022
CN202110680934.4A 2021-06-18 2021-06-18 Method for rapidly generating non-repeated random natural numbers Pending CN113778380A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202110680934.4A CN113778380A (en) 2021-06-18 2021-06-18 Method for rapidly generating non-repeated random natural numbers

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202110680934.4A CN113778380A (en) 2021-06-18 2021-06-18 Method for rapidly generating non-repeated random natural numbers

Publications (1)

Publication Number Publication Date
CN113778380A true CN113778380A (en) 2021-12-10

Family

ID=78835820

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202110680934.4A Pending CN113778380A (en) 2021-06-18 2021-06-18 Method for rapidly generating non-repeated random natural numbers

Country Status (1)

Country Link
CN (1) CN113778380A (en)

Similar Documents

Publication Publication Date Title
CN109783817B (en) Text semantic similarity calculation model based on deep reinforcement learning
CN110059875B (en) Public bicycle demand prediction method based on distributed whale optimization algorithm
Ben-Dor et al. On constructing radiation hybrid maps
CN113344113B (en) Yolov3 anchor frame determination method based on improved k-means clustering
CN111709415B (en) Target detection method, device, computer equipment and storage medium
CN107122573B (en) Automobile operation condition design method based on Markov chain evolution
CN111383157B (en) Image processing method and device, vehicle-mounted operation platform, electronic equipment and system
CN113778380A (en) Method for rapidly generating non-repeated random natural numbers
CN110097581B (en) Method for constructing K-D tree based on point cloud registration ICP algorithm
CN110019184A (en) A kind of method of the orderly integer array of compression and decompression
CN108805280A (en) A kind of method and apparatus of image retrieval
CN113705784A (en) Neural network weight coding method based on matrix sharing and hardware system
CN113611354A (en) Protein torsion angle prediction method based on lightweight deep convolutional network
CN116578425B (en) Load balancing method and system based on rasterization
CN117951308A (en) Zero sample knowledge graph completion method and device
CN117892714A (en) Text deduplication method and device, electronic equipment and storage medium
CN115481364A (en) Parallel computing method for large-scale elliptic curve multi-scalar multiplication based on GPU (graphics processing Unit) acceleration
CN110097505A (en) A kind of Law of DEM Data processing method and processing device
CN116226698A (en) Cell type identification method, system and equipment based on multi-group chemical data integration
CN116415144A (en) Model compression and acceleration method based on cyclic neural network
CN114077866A (en) Vector similarity calculation method and system based on ratio logarithm
CN115049042A (en) High-order network model generation method based on multiple small group structure embedding
CN114241470A (en) Natural scene character detection method based on attention mechanism
CN110807479A (en) Neural network convolution calculation acceleration method based on Kmeans algorithm
CN113434955A (en) Reconfigurable design method of ship information system based on digital twin virtual model

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination