The problem of large random number in C language

Source: Internet
Author: User
Tags rand

First, the random number obtained by RAND () is within a certain range, typically 0x7fff, which is 32767

So to take advantage of this function to take this large random number what to do, I think there is only one way, is splicing

Usually we make such a mistake: rand () *rand () so the feeling is not to take a larger random number, because random times random or random, but it is actually wrong

You can think of the rand () function as a repeating full arrangement, assuming there are now 6 numbers (the probability of getting the same)

Rand1 (): 1 2 3 4 5 6

Rand2 (): 1 2 3 4 5 6

So now look at the probability of the result (two examples)

Rand1 () *rand2 () P

2=1*2,2*1 1/6 * 1/6+1/6 * 1/6=2/36

12=3*4,4*3,2*6,6*2 1/6 * 1/6+1/6 * 1/6+1/6 * 1/6+1/6 * 1/6=4/36

It can be seen that this is undesirable, because the probability of acquiring a number is not the same, it certainly does not satisfy the concept of random (that is, each number is chosen to the same probability).

But stitching is desirable.

Like 2 and 12.

We can imagine two seats (RAND1 and Rand2, respectively).

___          ___

Each vacancy has the following options

0 0

1 1

2 2

3 3

4 4

5 5

6 6

7 7

8 8

9 9

So 2 is 0 2 so P (x=2) =1/10*1/10=1/100  

So 12 is 1 2 so P (x=12) =1/10*1/10=1/100

There is no doubt that stitching is no problem.

The problem of large random number in C language

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.