How does random in C # actually work?

Source: Internet
Author: User
Tags random seed

Look at the code first:

In the loop, there is only new one random, and some each time new one random.

1Console.WriteLine ("1. Multiple random, default randomly seeded,");2              for(inti =1; I <Ten; i++ )3             {4Random R1 =NewRandom ();5                 intv = R1. Next ( -);6Console.Write (v); Console.Write (" ");7System.Threading.Thread.Sleep ( -);8             }9Console.WriteLine ("\ n");Ten  OneConsole.WriteLine ("2. More than one random, specifying a randomly seeded"); A              for(inti =1; I <Ten; i++) -             { -Random r2 =NewRandom ( -); the                 intv = R2. Next ( -); -Console.Write (v); Console.Write (" "); -System.Threading.Thread.Sleep ( -); -             } +Console.WriteLine ("\ n"); -  +Console.WriteLine ("3. One random, default randomly seeded"); ARandom R3 =NewRandom (); at              for(inti =1; I <Ten; i++) -             { -                 intv = R3. Next ( -); -Console.Write (v); Console.Write (" "); -System.Threading.Thread.Sleep ( -); -             } inConsole.WriteLine ("\ n"); -  toConsole.WriteLine ("4. One random, specifying a randomly seeded"); +Random R4 =NewRandom ( -); -              for(inti =1; I <Ten; i++) the             { *  $                 intv = R4. Next ( -);Panax NotoginsengConsole.Write (v); Console.Write (" "); -System.Threading.Thread.Sleep ( -); the             } +Console.WriteLine ("\ n"); A  theConsole.read ();
View Code

Run the first time result:

Run the second result:

1. In the second case, the resulting random number is a fixed value
2. In the fourth case, a set of random numbers generated each time is a fixed value.
3. The first and third types are different from each other. (the default random seed).

Looking at MSDN, it also explains why the second and fourth scenarios occur.

 objects created within a millisecond may encounter the same seed value.

I've always felt that in the second and fourth cases, the same random number is the bug, knowing that I have the following situation today, I find this is really useful.
I wrote a program that frantically makes random numbers and inserts data directly into the database. Business data comes from fixed clients, and some clients may not produce data for days. In order for the data to look more realistic, it must be random that some clients do not produce data.
The generated client that does not produce data is random, such as C1,C2,C3. N days later, a batch of clients that become two-out does not produce data, such as C1,C4,C6,C9. (triggered by a timer in n days).
But there is a problem: When my program is restarted within n days, the random client changes. Not up to the requirements. I want the program to restart in n days, randomly out of the client list, does not change
At this point, "generating the same series of random numbers" is useful.
For example, use this method to generate random seeds:

1 Private intseedbydate ()2 {3 4DateTime now = DateTime.Now.AddDays (4);5DateTime Dtbegin =NewDateTime (DateTime.Now.Year,1,1);6 7 intDiffday = Now. DayOfYear%3;//3 days a loop8DateTime result = Dtbegin. AddDays (now. DayOfYear-diffday);9 return(int) result. Ticks;Ten}

How does random in C # actually work?

Related Article

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.