Some tests for rand () under Windows C + +

Source: Internet
Author: User
Tags random seed

intxs[]={-1,0,1,0};intys[]={0,-1,0,1};intx=0, y=0; for(intt=0;t<1<< -; t++){    intD=rand ()%4; X+=xs[d];y+=Ys[d]; Draw a point at (x, y);}

Depending on the value of rand ()%4, the direction of movement of the point is determined, and the image is generated as follows, and it can be found that no matter what the seed of the Srand () is set, the period is 218 or 262144

The resulting shape shapes are exactly the same, but the starting point is different in the diagram because of the random seed

RAND ()%8 has a period of 219

RAND ()%2n period is 2n+16 (0<n<=15)

Rand ()%3 and the number of other non-2 powers have a longer cycle time

The pseudo-random number generated by RAND () has its non-random symmetry and cyclic nature, which may affect the complexity or correctness of some algorithms that require true random numbers to satisfy expectations, but it is generally difficult to observe the effect of rand () 's regularity on many randomized algorithms.

Some tests for rand () under Windows C + +

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.