rahul bhosale

rahul bhosale

  • NA
  • 65
  • 12k

How do i generate unique random numbers

Jan 23 2009 4:42 AM

hi,

I want to generate unique random numbers,

suppose, N be number of random nos to generate and MIN and MAX is the range.

Then how do u i get those N numbers which are unique and random?

i hv code snippet like this,

int minNumber=0;

Random r = new Random();

// Use an list to store generated numbers

List<int> uniqueRandNos = new List<int>();

// Create the unique random numbers

do

{

int testNum = r.Next(minNumber, maxNumber);

// Have we added this value already?

if (!uniqueRandNos.Contains(testNum))

{

uniqueRandNos.Add(testNum);

}

} while (uniqueRandNos.Count < numberOfSelections);

return uniqueRandNos;

 

 

Whether this is feasible for real time systems....?


Answers (2)