10-digit random distribution algorithm

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.