Click here to Skip to main content
12,624,902 members (36,727 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.6K views
2 bookmarked
Posted

How to generate many random various numbers?

, 9 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Use LINQ:public IEnumerable GenerateRandomSequence(int min, int max){ var rnd = new Random(); return Enumerable.Range( min, max-min ).OrderBy( n => rnd.Next() );}You'll need a reference to System.Core to use this, and a version of .NET that supports LINQ.If you're using...

Use LINQ:

public IEnumerable<int> GenerateRandomSequence(int min, int max)
{
    var rnd = new Random();
    return Enumerable.Range( min, max-min ).OrderBy( n => rnd.Next() );
}

You'll need a reference to System.Core to use this, and a version of .NET that supports LINQ.

If you're using .NET 4, you can use multiple cores to parallelize it, just add ".AsParallel()" to the end of the Range function:

return Enumerable.Range( min, max-min ).AsParallel().OrderBy( n => rnd.Next() );


NOTE: AsParallel() is not a good way to accomplish this, as the Random class is not thread safe. For a parallel and thread safe solution, see the article mentioned in the comments below (Thanks to Richard Deeming for pointing this out! :) )



To use:

foreach(var number in GenerateRandomSequence(10,500))
{
   // Do something with number
   Console.WriteLine(number);
}

The above code generates a non-repeating random sequence of numbers between 10 and 500.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

myker
Software Developer (Senior) Woodland Furniture
United States United States
Started programming on a Commodore Vic-20 at 6 years old. Never really stopped since then. Fluent in x86 Assembly, C++ (ATL & MFC), C# (ASP.NET/WF/WPF/Silverlight), HTML, & Javascript. Really excited at the fast pace at which new technology is released.

You may also be interested in...

Comments and Discussions

 
GeneralRe: After running tests, I have to conclude that Richard is corr... Pin
myker9-Sep-11 13:25
membermyker9-Sep-11 13:25 
GeneralRe: Never mind. I must have written that comment before drinking... Pin
Henry.Ayoola5-Sep-11 5:03
memberHenry.Ayoola5-Sep-11 5:03 
GeneralReason for my vote of 5 Creative.. Pin
Pravin Patil, Mumbai13-Sep-11 1:29
memberPravin Patil, Mumbai13-Sep-11 1:29 
GeneralThe Random class is not thread-safe, so adding ".AsParallel(... Pin
Richard Deeming9-Sep-11 9:41
memberRichard Deeming9-Sep-11 9:41 
GeneralRe: Good catch, Richard! That's really good to know. I'm remov... Pin
myker9-Sep-11 12:40
membermyker9-Sep-11 12:40 
GeneralRe: Richard... after further investigation, the AsParallel() is ... Pin
myker9-Sep-11 12:51
membermyker9-Sep-11 12:51 
GeneralHowever, it doesn't give a uniform distribution. Pin
Henry.Ayoola2-Sep-11 5:31
memberHenry.Ayoola2-Sep-11 5:31 
GeneralRe: I'm not sure I understand your comment. Can you explain fur... Pin
myker2-Sep-11 6:12
membermyker2-Sep-11 6:12 
GeneralReason for my vote of 5 Nice approach using LINQ Pin
Shahin Khorshidnia1-Sep-11 15:32
memberShahin Khorshidnia1-Sep-11 15:32 
GeneralRe: Thank you! Glad to be of some help. Pin
myker1-Sep-11 18:30
membermyker1-Sep-11 18:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161128.1 | Last Updated 9 Sep 2011
Article Copyright 2011 by myker
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid