10 numbers are randomly divided in 10 locations, so how to write a random algorithm.
My idea: Generate a random number, then 10 positions 10 modulo + random number, but feel not enough random, there is nothing good algorithm!
Like what:
1 2 3 4 5 6 7 8 9 10
2 1 4 3 9 7 8 6 5 10
。。。
(randomly arranged)
Add:
Provides a method:
10-digit linear table, random (10) = 5, delete node 5
9-digit linear table, random (9) = 2, delete node 2
。。。。
Last random sequence 5, 2 .....
It feels so random.
Reply content:
10 numbers are randomly divided in 10 locations, so how to write a random algorithm.
My idea: Generate a random number, then 10 positions 10 modulo + random number, but feel not enough random, there is nothing good algorithm!
Like what:
1 2 3 4 5 6 7 8 9 10
2 1 4 3 9 7 8 6 5 10
。。。
(randomly arranged)
Add:
Provides a method:
10-digit linear table, random (10) = 5, delete node 5
9-digit linear table, random (9) = 2, delete node 2
。。。。
Last random sequence 5, 2 .....
It feels so random.
Pseudo code:
生成长度为10的数组a,压入1~10(或其他你想要的数)for (i = a.length - 1; i > 0; i--) { 生成一个0~i的随机数j(0 <= j < i) 交换a[i]和a[j]}
The computer has no way to produce a real random number ... Only as far as possible the resulting number distribution evenly in each interval.
is not the problem of random algorithm, is the sample is too small, you randomly 100 numbers to try, it looks very random
Randomly come out 1, 3, 2, 4, 5, 6, 7, 8, 10, 9, you still don't think it's random enough.
You want to scatter 1-10 10 numbers to 10 positions. The hash is OK.