14-character Random number generator

Source: Internet
Author: User
Tags random seed

A recent project reminds me of a very simple way to generate a pseudo-random number to create a raindrop's behavior. I found that if we had a randomly initialized unsigned 32-bit seed n could loop through a pseudo-random number (C) using only 14-character multiplication tables: n*=0x9e3779b1;

Each time you apply this action, N will contain a new pseudo-random number. Very mysterious, c this operation is actually a coarse hash function of the integer multiplication method called Knuth, in an unsigned integer overflow for the moduolo operation. Moreover, for any good hash function, the result of repeated use of the hash itself produces a pseudo-random value stream in nature. Of course, this is not a good hash function, so reusing this hash itself will quickly worsen the quality of randomness, so obviously you should not use in production code-but it applies to any non-critical, such as code poems!

The magical digital 0x9e3779b1 is just a large prime number specified in the system. Technically, it can replace any other large prime. Most importantly, it must have several factors, and there is a large enough amount of information to allocate to a higher value bit when an integer overflows. This is mostly the nearest prime number 2 ^ 32/φ-Knuth Initial selection
In the water. I did some extra bad things (TM) such as using a 64-bit signed integer, using an uninitialized value as the starting value of a random seed, both of which are undefined behaviors, but fortunately gcc kindness for me still has an impact I've been looking for generating code. Antique Identification

14-character Random number generator

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.