Click here to Skip to main content
15,879,239 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

How to generate many random various numbers?

Rate me:
Please Sign up or sign in to vote.
4.27/5 (9 votes)
9 Sep 2011CPOL 15.7K   2   10
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:


C#
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:


C#
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:


C#
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)


Written By
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.

Comments and Discussions

 
GeneralRe: After running tests, I have to conclude that Richard is corr... Pin
myker9-Sep-11 12:25
myker9-Sep-11 12:25 
GeneralRe: Never mind. I must have written that comment before drinking... Pin
Henry.Ayoola5-Sep-11 4:03
Henry.Ayoola5-Sep-11 4:03 
GeneralReason for my vote of 5 Creative.. Pin
Pravin Patil, Mumbai13-Sep-11 0:29
Pravin Patil, Mumbai13-Sep-11 0:29 
GeneralThe Random class is not thread-safe, so adding ".AsParallel(... Pin
Richard Deeming9-Sep-11 8:41
mveRichard Deeming9-Sep-11 8:41 
GeneralRe: Good catch, Richard! That's really good to know. I'm remov... Pin
myker9-Sep-11 11:40
myker9-Sep-11 11:40 
GeneralRe: Richard... after further investigation, the AsParallel() is ... Pin
myker9-Sep-11 11:51
myker9-Sep-11 11:51 
GeneralHowever, it doesn't give a uniform distribution. Pin
Henry.Ayoola2-Sep-11 4:31
Henry.Ayoola2-Sep-11 4:31 
GeneralRe: I'm not sure I understand your comment. Can you explain fur... Pin
myker2-Sep-11 5:12
myker2-Sep-11 5:12 
GeneralReason for my vote of 5 Nice approach using LINQ Pin
Shahin Khorshidnia1-Sep-11 14:32
professionalShahin Khorshidnia1-Sep-11 14:32 
GeneralRe: Thank you! Glad to be of some help. Pin
myker1-Sep-11 17:30
myker1-Sep-11 17: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.