Method for rapidly generating non-repeated random natural numbers

文档序号:378159 发布日期:2021-12-10 浏览:4次 中文

阅读说明:本技术 一种不重复随机自然数的快速生成方法 (Method for rapidly generating non-repeated random natural numbers ) 是由 王璞 张亮 高舜龙 宋兴瑞 罗英 季尧遥 张海丰 于 2021-06-18 设计创作,主要内容包括:本发明公开了一种不重复随机自然数的快速生成方法,定义两个初值为空的集合S和集合T,定义一个长度为M的一维数组F,并将F中每一个元素的数值初始化为1,即F[1:M]=1。循环N次调用随机生成函数,第k次调用随机生成函数的Ω区域为Ω-(k)=[1,M-k+1],其中k=1,2,…,N,将每一个随机生成数S-(k),按顺序存储在S中。依次循环集合S中的每个元素S-(k)(k=1,2,…,N),计算出满足的第一个索引J,将索引J添加到集合T中,并令F[J]=0。返回集合T。此算法解决了在给定的自然数区域内,能够快速挑选出一部分不重复的自然数序列。(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 omega k =[1,M‑k+1]Where k is 1,2, …, N, each randomly generating a number S k And stored in S in order. Sequentially cycling each element S in the set S k (k is 1,2, …, N), calculated to satisfy 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.)

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 satisfyAdd 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

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:

5. the method as claimed in claim 4, wherein the algorithm calculation is substituted to obtain: if rememberThen

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:

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:

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 satisfyAdd 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

Preferably, the method for rapidly generating random numbers without repeating according to claim 3, wherein the calculation amount of the algorithm is:

preferably, the above-mentioned calculation amount of the algorithm is calculated by substituting: if rememberThen

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:

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:

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 satisfyAdd 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 isThe calculation amount of the algorithm is as follows:

the calculation amount of the algorithm is obtained by substituting calculation: if rememberThen

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:

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

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 satisfyAdd 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 byThe calculation amount of the algorithm is as follows:

the calculation amount of the algorithm is obtained by substituting calculation: if rememberThen

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:

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

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 satisfyAdd 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, satisfyingSo 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, satisfyingSo 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, satisfyingSo 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 9So 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 satisfyAdd 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 isThe calculation amount of the algorithm is as follows:

the calculation amount of the algorithm is obtained by substituting calculation: if rememberThen

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:

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

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.

8页详细技术资料下载
上一篇:一种医用注射器针头装配设备
下一篇:一种PPTL规范挖掘系统和方法

网友询问留言

已有0条留言

还没有人留言评论。精彩留言会获得点赞!

精彩留言,会给你点赞!

技术分类