Random number generator C Language

Source: Internet
Author: User

I have read 90% of the mathematical analysis, but I have not studied it in depth. This code is copied, and the core part is copied. It feels good.

Generate a fixed random number sequence, as long as the seeds are the same. This random number generator is transparent and easy to change. It is good.

Every time you implement the code, you will find a lot of problems. And, once you write the code, time will fly... very good.

Okay, post it. I'm here for an Internet cafe, and I'm writing a blog while playing a movie. Haha. It's good.

/* 10-35-11-2-08-20.11.c -- Chapter 10, question 1 */<br/> # include <stdio. h> </P> <p> # define false (0) <br/> # define true (1) <br/> # define a (48271l) <br/> # define M (2147486647l) <br/> # define Q (m/a) <br/> # define R (M %) </P> <p> unsigned long int seed; </P> <p> int main (void); <br/> int eatenter (void ); <br/> void initialize (unsigned long int initial_value); <br/> double random (void); </P> <p> int main (void) <br/ >{< Br/> unsigned long int initial_value; <br/> char ch; </P> <p> printf ("Please input a initial value (> = 0, enter 'q' to quit .): "); <br/> while (scanf (" % lu ", & initial_value )! = 0) <br/>{< br/> printf ("initial value has been initialized is: % lu/N", initial_value); <br/> eatenter (); <br/> initialize (initial_value); <br/> puts ("Enter 'q' to input a new initial value and enter any key else to generate a random value again. "); <br/> while (CH = getchar ())! = 'Q') <br/>{< br/> If (true = eatenter () <br/> break; <br/> printf ("It is % lf. /n ", random (); <br/>}< br/> printf (" Please input a initial value (> = 0, enter 'q' to quit .): "); <br/>}</P> <p> return 0; <br/>}</P> <p> int eatenter (void) <br/>{< br/> If ('q' = getchar () <br/> return true; <br/> else <br/> return false; <br/>}</P> <p> void initialize (unsigned long int initial_value) <br/>{< br/> seed = initial_value; <br/>}</P> <p> double random (void) <br/>{< br/> unsigned long int temp_seed; </P> <p> temp_seed = A * (seed % q)-R * (seed/Q); <br/> If (temp_seed> = 0) <br/> seed = temp_seed; <br/> else <br/> seed = temp_seed + m; </P> <p> return (double) seed/m; <br/>}

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.