Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

How to generate many random various numbers?

By , 9 Sep 2011
Rate this:
Please Sign up or sign in to vote.

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! Smile | :) )


 

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)

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.
Follow on   Twitter

Comments and Discussions

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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.8.140421.2 | Last Updated 9 Sep 2011
Article Copyright 2011 by myker
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid