2

I want to generate random (double) numbers between two limits, let say: lim1 and lim2.

But, I want this numbers to be generated in order. E.g.: between 1 and 6 : 1.53412 1.654564 2.213123 5.13522 . Thanks!

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
Florin M.
  • 2,159
  • 4
  • 39
  • 97

3 Answers3

4
public static double[] GenerateRandomOrderedNumbers(double lowerBoundInclusive, double upperBoundExclusive, int count, Random random = null)
{
    random = random ?? new Random();
    return Enumerable.Range(0, count)
        .Select(i => random.NextDouble() * (upperBoundExclusive - lowerBoundInclusive) + lowerBoundInclusive)
        .OrderBy(d => d)
        .ToArray();
}

Not perfect, but I hope this puts you in the right direction.

Allon Guralnek
  • 15,813
  • 6
  • 60
  • 93
1

Generate the random numbers and put them on a list:

var numbers = new List<int>();
Random random = new Random();

Add your numbers:

var number = random.Next(min, max); 
numbers.Add(number);

Then sort the list:

var orderList = from n
  in numbers
  orderby n
  select n;
Ulises
  • 13,229
  • 5
  • 34
  • 50
0

What about using this to generate a set of random numbers:

lim1 + random.Next(lim2 - lim1)

and then simply sorting them?

See also:

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674