I know that there is nothing that does not repeat in the cycle~, and the repetition rate can only be reduced, but I still have to give it a try~, are there any Taoist friends who can give me a try~
仅有的幸福2017-06-28 09:25:14
I think the meaning of randomness is that the two result values before and after are unpredictable. Generally speaking, knowing A1 A2 cannot deduce the meaning of A3.
The problem of repeated values you mentioned should be measured from the perspective of probability. If the probability of obtaining each value is equal, it means "random". Repeating the results twice does not mean that it is not "random" enough.
If you must strictly limit the value to be non-duplicate, you need a storage place, and this non-duplication must also have a time or space range.
某草草2017-06-28 09:25:14
I previously saw an algorithm for randomly generating non-repeating numbers on CSDN:
1 2 3 4 5 6 7 8 9 |
|
阿神2017-06-28 09:25:14
A stupid way is to directly initialize the array, put each number in the correct position, and then randomly select it. The first result is exchanged with the last digit, and the second result is exchanged with the second to last digit. Of course, after the exchange, a random selection will be made next time The range of the number will also be reduced by one accordingly.
As for the efficiency, the poster can implement it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
|
大家讲道理2017-06-28 09:25:14
If the life cycle is long, 100,000 will be used up easily. After using up, it will be repeated no matter how you generate it.
The simplest, Math.random()*10_0000, and then convert to int. There is no guarantee that it will not be repeated
increment, similar to the primary key increment of mysql. Starting from 1, if there are less than six digits, use 0 to make up the front
Advanced point. The six-digit number can be combined using various influencing factors. Of course, this number is relatively short and there are not many factors to consider.
Finally, give a reference link, distributed system ID generation. This may not be related to your problem, but it also has some reference.
怪我咯2017-06-28 09:25:14
1 |
|
0 to 100 is the range, 6 is the number. What's more important is thread safety.