Click here to Skip to main content
14,208,504 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 1 Sep 2011
Licenced CPOL

How to generate many random various numbers?

, 9 Sep 2011
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...


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

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


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


About the Author

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
membermyker9-Sep-11 12:25 
GeneralRe: Never mind. I must have written that comment before drinking... Pin
Henry.Ayoola5-Sep-11 4:03
memberHenry.Ayoola5-Sep-11 4:03 
GeneralReason for my vote of 5 Creative.. Pin
Pravin Patil, Mumbai13-Sep-11 0:29
memberPravin 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
membermyker9-Sep-11 11:40 
GeneralRe: Richard... after further investigation, the AsParallel() is ... Pin
myker9-Sep-11 11:51
membermyker9-Sep-11 11:51 
GeneralHowever, it doesn't give a uniform distribution. Pin
Henry.Ayoola2-Sep-11 4:31
memberHenry.Ayoola2-Sep-11 4:31 
GeneralRe: I'm not sure I understand your comment. Can you explain fur... Pin
myker2-Sep-11 5:12
membermyker2-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
membermyker1-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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06 | 2.8.190612.1 | Last Updated 9 Sep 2011
Article Copyright 2011 by myker
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid