Home > Back-end >  C not repeat big random number problem?
C not repeat big random number problem?

Time:09-17

C + + is there a better way to generate a random number, range between 0 ~ INT_MAX,
Focus is not repeated, until each number is generated within the scope of this again so far?

CodePudding user response:

Generate a random number too simple needn't discuss
The difficulty is to avoid repeat in order to avoid repeat you must save all data have been generated
Range is too big without opportunistic space
32-bit application is very difficult to do this even if the scope of memory use bitmap stored data generated has
Either on a 64 - bit program and then do
Or save to other peripheral storage (file or database or network)
The program to run visual will slow the

CodePudding user response:

The problem of your original post
I feel the problem is you think half success

CodePudding user response:

Not to repeat generally will not require a wide range of random number
Such as a database record serial number sequence number

CodePudding user response:

refer to the second floor lin5161678 response:
your original question posted
I feel the problem is you think half success

Original problem is a problem of random write full wholesale:
A maximum capacity can be 10 t, I will write a full, the random to make sure every sector only write once, fixed every time to write the number of sectors,
Subsequent may need time to write the length of the also is random,
This is my current demand, I don't know how convenient implementation, just thinking about it will not be repeated random number, you have what better way?

CodePudding user response:

refer to the second floor lin5161678 response:
your original question posted
I feel the problem is you think half success

Add question: I wrote this random plateful of expression is not very accurate, concrete is given a start sector and a length of operation, in the start sector operating within the length of the random write full back, ensure that each of these sectors written only once, not repeat, and length of the operation may be a full size, which is completely full,
I think of a solution is to use a random number engine to generate random address, intermediate state established by using a hash table, and then after the random write full 70% or 80%, random, the rest of the order fill directly,
The second is my husband as a configuration file, will be randomly generated address written to a file, the software to read the address again,
But these two methods are needed to establish storage sector state, intermediate process
So want a better way,

CodePudding user response:

To random cannot guarantee not to repeat, promise not to repeat is not random, but shuffle

CodePudding user response:

If we know that the random scope, then put the scope in a collection, with the length of the collection to seed the random collection of an element, and delete elements in the set, repeat this process, until set no elements, such, can not only ensure the random, can guarantee not to repeat again,
  • Related