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

Tagged as

How to generate many random various numbers?

, 6 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Assuming the range of numbers is small, and all of them are required in the result (those are the assumptions implied by your code), and with a small change in the API definition, a much more compact and faster implementation would be this:public IList randomPermutation(int min, int max)...
Assuming the range of numbers is small, and all of them are required in the result (those are the assumptions implied by your code), and with a small change in the API definition, a much more compact and faster implementation would be this:
public IList<int> randomPermutation(int min, int max) {
    Random r=new Random();
    SortedList<int, int> perm=new SortedList<int, int>();
    for (int i=min; i<=max; i++) perm.Add(r.Next(), i);
    return perm.Values;
}
 
BTW: If the size of the number set were to increase above 100, I would consider switching to a Dictionary<int,int> and sorting it at the end (the code shown spends cycles keeping the SortedList<int,int> sorted while it is being populated).
 
Smile | :)

License

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

Share

About the Author

Luc Pattyn
Software Developer (Senior) Perceler
Belgium Belgium
I am an engineer with a background in electronics, software and mathematics.
 
I develop technical software, both for embedded systems and for desktop equipment. This includes operating systems, communication software, local networks, image processing, machine control, automation, etc.
 
I have been using all kinds of microcontrollers and microprocessors (Intel 4004/8080/8051/80386/Pentium, Motorola 680x/680x0/ColdFire/PowerPC, Microchip PIC, Altera NIOS, and many more), lots of programming languages (all relevant assemblers, Fortran, Basic, C, Java, C#, and many more), and different operating systems (both proprietary and commercial).
 
For desktop applications and general development tools I have been using both UNIX systems and Mac/MacOS for many years, but I have switched to x86-based PCs with Windows, Visual Studio and the .NET Framework several years ago.
 
I specialize in:
- cross-platform development (making software that runs on diverse hardware/OS combinations)
- instruction set simulation
- improving software performance, i.e. making sure the software runs the job at hand in as short a time as possible on the given hardware. This entails algorithm selection, implementation design, accurate measurements, code optimisation, and sometimes implementing virtual machines, applying SIMD technology (such as MMX/SSE), and more.

Comments and Discussions

 
SuggestionAnother alternative PinmemberRemcoReitsma6-Aug-14 4:58 
GeneralNice code, Luc. PinsubeditorWalt Fair, Jr.31-Aug-11 9:31 
GeneralRe: Thanks Walt. I do like compact code, unless readability woul... PinmvpLuc Pattyn31-Aug-11 13:02 
Generalwill it generate unique no. ? PinmemberPradip_Bobhate31-Aug-11 5:54 
GeneralRe: yes PinmvpLuc Pattyn31-Aug-11 7:00 

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 | Terms of Use | Mobile
Web01 | 2.8.1411028.1 | Last Updated 6 Sep 2011
Article Copyright 2011 by Luc Pattyn
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid