Specific embodiment
Pseudo-random algorithm to generate random number using recurrence formula, and according to the difference of specific algorithm, recursion is public
Formula can be single order or second order or multistage.Taking second order as a example, its recurrence formula such as formula 1
Shown:
xn+2=f (xn+1, xn), n >=0 formula 1
In formula 1, xn、xn+1、xn+2For random factor.To second order recursive algorithm, x0And x1For initial value.
The random number sequence being generated is as shown in Equation 2:
rk=g (xk), k >=2 formula 2
According to the demand of concrete application scene, can be by random factor directly as random number (this being generated
When rk=xk);Corresponding random number can also be obtained to after the conversion of random factor doing mathematicses.
Not all of recursive algorithm may serve to generate random number, is only generated with recursive algorithm
Sequence require through the check character a little statistics of unification, such as uniformity, randomness of sampling etc. meet certain
Condition, such recursive algorithm just can be used as pseudo-random algorithm.That is, pseudo-random algorithm
Started with certain initial value, recursion generates a series of random factors several times, these random factors are substantially not
The situation having repetition occurs.
So, when L random number to be generated on first node in cluster it is meant that recursion is at this
To carry out L time on node, if second node with after this node recursion at least L time gained random because
As initial value, then the initial value of second node substantially will not be identical with the initial value of first node for son.
This mode is generalized to for generating all nodes of random number in cluster, then the initial value of each node
All substantially will not be identical with the initial value of other nodes.
Based on above-mentioned thinking, embodiments herein proposes a kind of generation method of new pseudo random number,
The predetermined storage location being available for each node visit in cluster preserves initial value, predetermined from this in each node
After storage location reads initial value, carry out obtaining this node after recursive operation several times will not continue to for passing
Push the random factor of calculation, write predetermined storage location as initial value, so that other nodes use,
Thus be substantially able to ensure that each node initial value different from other nodes, to solve in prior art
The problem existing.
Cluster in the embodiment of the present application includes at least two nodes, and each node is an operation pseudorandom
Algorithm generates the unit of random number, and for example, node can be a physical server, a logical device
Program example of (as virtual machine or simulator) or an independent operating etc..The enforcement of the application
In example, the method for operation of cluster and networking structure, the species of node and communication mode etc. are not all limited.
In the embodiment of the present application, the flow process of the generation method of the pseudo random number run on each node is as schemed
Shown in 1.
Step 110, obtains from predetermined storage location and starts random factor.
In embodiments herein, predetermined storage location can be by all sections of operation pseudo-random algorithm in cluster
Point accesses, and these nodes all have the authority of read-write to the content being saved in predetermined storage location.In advance
Determining storage location can be it is also possible on the equipment outside cluster, not do on certain node in the cluster
Limit.For example,
Start random factor to be used by the nodes to as initial value, input recurrence formula to generate random number sequence.
Run on each node in cluster at the beginning of the pseudo-random algorithm of identical exponent number, the pseudo-random algorithm of different rank
Initial value number is different.The number starting random factor is identical with the exponent number of the pseudo-random algorithm running on node,
The number of the value that predetermined storage location is preserved is identical with the number of the startup random factor of each node.When
When the number starting random factor is more than one, each node is according to same order by predetermined storage location
The value preserving gives each and starts random factor.
Step 120, according to the number upper limit starting random factor and this node random number to be generated, determines
Stop random factor, and random factor will be stopped to write described predetermined storage location.
The number upper limit assuming this node random number to be generated is N (N be natural number), is depositing from predetermined
After storage space puts reading startup random factor, node, to start random factor as initial value, uses pseudo-random algorithm
In recurrence formula carry out at least K time recursion (K is not less than N), if the used pseudo-random algorithm of node
Exponent number m (m be natural number), the k-th random factor that recursion is obtained is individual to (K-m-1)
Random factor is as termination random factor.Node will stop random factor write predetermined storage location, will be pre-
Determine the original value of storage location to be updated to stop random factor.When stopping random factor more than one, often
Individual node according to same order when predetermined storage location reads and starts random factor, will stop at random
The factor writes predetermined storage location.
For example, to the pseudo-random algorithm using single order recursive algorithm, start random factor and stop random because
Son all only one of which, node, to start random factor as initial value, runs n times pseudo-random algorithm, obtains
To n-th random factor.The number upper limit due to this node random number to be generated is N, and this node will not
Again with n-th random factor come recursion (N+1) individual random factor, therefore this node can be by N
Individual random factor, as stopping random factor, is written to predetermined storage location, for other nodes as startup
Random factor uses.
In application scenes, whole cluster is needed to be generated using certain pseudo-random algorithm and is less than certain
The random number of this quantification now can will be given birth to as on each node by the random number of quantification
Become the number upper limit of random number.If whole cluster random number quantity to be generated is difficult to determine, every
On individual node, the number upper limit of random number to be generated also is difficult to determine, now can make each node according to
Generating random number, every batch generates a number of random number to batch, when a batch generate random
When number reaches this quantity, node reacquires initial value from predetermined storage location.Specifically, at every batch
Secondary is initial, and node obtains from predetermined storage location and starts random factor, and this batch volume residual is put
For this batch number upper limit;Node, to start random factor as initial value, needs to generate at random each
Run a pseudo-random algorithm during number and obtain next random factor, and this batch volume residual is subtracted one,
Until this batch volume residual is zero;When this batch volume residual is zero, node starts next batch,
Reset this batch volume residual from predetermined storage location acquisition startup random factor and with this batch number upper limit.
It should be noted that this batch number upper limit of each batch of node can identical it is also possible to different.
This batch number upper limit can be arranged in advance manually by manager, can be transported according to cluster by cluster controller
Row situation determines and notifies node, can also be automatically generated according to self-operating situation by node, the application
Embodiment do not limit.When this batch number upper limit of each batch of node may be different, node can
When this batch volume residual is zero, to obtain this batch number higher limit of next batch.
Start random factor being read by predetermined storage location from a node, to by predetermined storage location more
New is the interior during the visit of termination random factor, and other nodes may be had to wish to obtain from predetermined storage location
Initial value.In order to avoid now other nodes obtain identical initial value, node can be from predetermined storage
Before position acquisition starts random factor, predetermined storage location is locked;And random factor write will stopped
After predetermined storage location, predetermined storage location is unlocked.So, other nodes only will be pre- in this node
Determine storage location value update finish after, just can read initial value.
It should be noted that the realization of predetermined storage location is different in different concrete application scenes, if
In scene, the access mechanism of predetermined storage location has been provided for locking and unlocking function in itself, for example predetermined
Storage location is certain record in the addressable data base of each node, then be no longer necessary to predetermined storage
Position separately locks and unlocks.
Step 130, to start random factor as initial value, generates according to pseudo-random algorithm and is less than institute
State the random number of the number upper limit.
After obtaining starting random factor, when needing to generate random number, in this node random number to be generated
The number upper limit in, node run pseudo-random algorithm, gradually recursion generate random number sequence.
In the realization that each node to generate random number according to batch, this node is in random number to be generated
In this batch number upper limit, run pseudo-random algorithm and generate random number sequence.
It can be seen that, in embodiments herein, it is available for the predetermined storage location of each node visit in the cluster
Preserve the initial value running pseudo-random algorithm, the value preserving is updated after obtaining initial value by each node
For unduplicated value, so that other nodes use as initial value.So enabling to each node makes
Generate random number with different initial values, greatly reduce that different nodes generate same random numbers can
Can property, it is to avoid because the cluster speed of service that initial value repeats to lead to declines, and cluster pair can be shortened
The response time of user.
If additionally, the pseudo-random algorithm running on each node in cluster is different, or running identical
Pseudo-random algorithm but the parameter that adopted in addition to initial value is different, what whole cluster was generated is all random
For number, its statistical properties is often not so good as the random number sequence that on a node, recursion generates.And this Shen
In embodiment please, when operation identical pseudo-random algorithm on each node in cluster, and except initial
Value is outer, when the other specification of the pseudo-random algorithm that each node runs is identical, each node in whole cluster
The random number generating is equivalent to the random number sequence generating on a node, can reach good statistics
Index, improves the quality of the random number of cluster generation.
In an application example of the application, cluster includes multiple nodes, and each node is a jvm
(Java Virtual Machine, Java Virtual Machine) example.This cluster is used for the request of receive user,
User is replied to as response after generating random password.Each jvm example by linear congruent algorithm Lai
Generate random number sequence, and in addition to initial value, on each jvm example, linear congruent algorithm is adopted
Other specification is identical.
Precalculated position in the addressable data base of all jvm examples, preserves linear congruence in the cluster
The initial value of algorithm random factor.The table for storing initial value is set up in data base, a kind of possible
Structure is as shown in table 1:
ID |
Gmt_create |
Gmt_modified |
Seed |
1 |
2015/4/4 |
2015/4/4 |
0 |
Table 1
In table 1, ID is line identifier;Gmt_create is the time of origin creating this record;
Gmt_modified is the time of origin of the last modification of this record;Seed is currently preserved random
The initial value of the factor.
Each jvm example to generate random number according to batch, and each jvm example presets every batch fortune
Places number constant T, namely the number upper limit of random number that every batch can generate.
In startup optimization, on each jvm example, the value of remaining number of run variable is 0.Receive each
To user random password ask when, the operational process on each jvm example is as shown in Figure 2.
Step 201, the random password request of receive user.
Step 202, judges whether the remaining number of run of this batch is zero, if it is, execution step 203;
If not, going to step 207;
Step 203, the precalculated position from data base is read and is started random factor.Access in jvm example
During this record in data base, this record is locked by data base.
Step 204, will start random factor as initial value, and execute T sublinear congruence algorithm, will pass
The T random factor released is as termination random factor.
Step 205, will stop the precalculated position in random factor write into Databasce, updates this record.?
After record renewal is finished by jvm example, this record is unlocked by data.
Step 206, a upper random factor variable is set to startup random factor, by remaining for this batch fortune
Places number is set to T.
Step 207, draws this random factor by a upper random factor recursion, by upper one random because
Sub- variable is set to this random factor, and remaining number of run is subtracted one.
Step 208, generates random password according to the random factor that this runs gained, asks as to user
The response asked returns to user.
Corresponding with the realization of above-mentioned flow process, embodiments herein additionally provides one kind and applies on clustered node
Pseudo random number generating means.This device can be realized it is also possible to by hardware or soft by software
The mode of combination of hardware is realized.As a example implemented in software, as the device on logical meaning, it is cluster section
The CPU (Central Process Unit, central processing unit) of point place equipment is by corresponding computer journey
Sequence instruction reads and runs formation in internal memory.For hardware view, except the CPU shown in Fig. 3,
Outside internal memory and nonvolatile memory, the equipment that the generating means of pseudo random number are located generally also includes
For realizing other hardware such as board of network communicating function.
Fig. 4 show a kind of generating means of pseudo random number of the embodiment of the present application offer, applies in cluster
In node on, described device includes initial value acquiring unit, initial-value generating unit and generating random number
Unit, wherein:Initial value acquiring unit is used for obtaining startup random factor from predetermined storage location;Initially
Value signal generating unit is used for, according to the number upper limit starting random factor and this node random number to be generated, determining
Stop random factor, and random factor will be stopped to write described predetermined storage location;Random number generation unit
For to start random factor as initial value, being generated according to pseudo-random algorithm and being less than the described number upper limit
Random number.
Optionally, described initial-value generating unit specifically for:To start random factor as initial value,
Run pseudo-random algorithm described in n times, using the n-th obtaining random factor as termination random factor, and
Random factor will be stopped and write described predetermined storage location;N is in the number of this node random number to be generated
Limit.
Optionally, the described number upper limit is in this section this batch of point this batch number of random number to be generated
Limit;Described device also includes volume residual reset cell and volume residual updating block, wherein:Remainder
Amount reset cell is used for after obtaining and start random factor from predetermined storage location, by this batch volume residual
It is set to this batch number upper limit;Volume residual updating block is used for after generating each random number, incite somebody to action this
Batch volume residual subtracts one, until this batch volume residual is zero;Described initial value acquiring unit is specifically used
In:When this batch volume residual is zero, obtains this batch from predetermined storage location and start random factor.
Optionally, described device also includes locking unit and unlocking unit, wherein:Lock unit to be used for
Before predetermined storage location obtains and starts random factor, predetermined storage location is locked;Unlocking unit is used for
After stopping random factor write predetermined storage location, predetermined storage location is unlocked.
Optionally, each node in described cluster runs identical pseudo-random algorithm;In addition to initial value,
The other specification that described pseudo-random algorithm adopts is identical.
The foregoing is only the preferred embodiment of the application, not in order to limit the application, all this
Within the spirit of application and principle, any modification, equivalent substitution and improvement done etc., should be included in
Within the scope of the application protection.
In a typical configuration, computing device includes one or more processors (CPU), input/output
Interface, network interface and internal memory.
Internal memory potentially includes the volatile memory in computer-readable medium, random access memory
(RAM) and/or the form such as Nonvolatile memory, such as read only memory (ROM) or flash memory (flash RAM).
Internal memory is the example of computer-readable medium.
Computer-readable medium include permanent and non-permanent, removable and non-removable media can by appoint
What method or technique is realizing information Store.Information can be computer-readable instruction, data structure, program
Module or other data.The example of the storage medium of computer include, but are not limited to phase transition internal memory (PRAM),
Static RAM (SRAM), dynamic random access memory (DRAM), other kinds of with
Machine access memorizer (RAM), read only memory (ROM), Electrically Erasable Read Only Memory
(EEPROM), fast flash memory bank or other memory techniques, read-only optical disc read only memory (CD-ROM),
Digital versatile disc (DVD) or other optical storage, magnetic cassette tape, tape magnetic rigid disk stores or it
His magnetic storage apparatus or any other non-transmission medium, can be used for storing the letter that can be accessed by a computing device
Breath.Define according to herein, computer-readable medium does not include temporary computer readable media (transitory
Media), as data signal and the carrier wave of modulation.
Also, it should be noted term " inclusion ", "comprising" or its any other variant be intended to non-
The comprising of exclusiveness, so that include a series of process of key elements, method, commodity or equipment not only wrap
Include those key elements, but also include other key elements being not expressly set out, or also include for this process,
Method, commodity or the intrinsic key element of equipment.In the absence of more restrictions, " included by sentence
One ... " key element that limits is being it is not excluded that including the process of described key element, method, commodity or setting
Also there is other identical element in standby.
It will be understood by those skilled in the art that embodiments herein can be provided as method, system or computer journey
Sequence product.Therefore, the application can using complete hardware embodiment, complete software embodiment or combine software and
The form of the embodiment of hardware aspect.And, the application can adopt and wherein include calculating one or more
Machine usable program code computer-usable storage medium (including but not limited to disk memory, CD-ROM,
Optical memory etc.) the upper computer program implemented form.