0

I have tried two different ways to get a sequence of random numbers:

// (a) works
Random random = new Random();
return Enumerable.Range(0, 20).OrderBy(n => random.Next());

// (b) does not work
return Enumerable.Range(0, 20).OrderBy(n => new Random().Next());

The first variant, where I am using random.Next(), is working fine.

But the variant, where I have call new Random().Next(), does NOT return random numbers; instead it returns a sequence of numbers from 0 to 20.

Now my questions are:

  • What is term to denote the second type of initialization of object new Random().Next() in C#?
  • And how it is different from the first one so that I am not getting the desire output?
Jitender Kumar
  • 2,439
  • 4
  • 29
  • 43
  • 1
    Btw. what is the purpose of the `OrderBy` random numbers? Why not just return random numbers to begin with? For example, to get a sequence of `n` random numbers in the range `[a..b)`, do `Enumerable.Range(1, n).Select(_ => random.Next(a, b))`. – stakx - no longer contributing Jul 03 '14 at 07:11
  • lol...seriously, mate! You're joking, aren't you? What's the point in getting random numbers between 0 and 20 and then sorting them!!!! Does it even make sense? – Leo Jul 03 '14 at 07:15
  • 1
    @stakx - your example can produce duplicates, which is different from provided example. – Alexander Jul 03 '14 at 07:16
  • @Leo that isn't what the code does, and yes it does make some sense (even if it isn't necessarily the ideal way to do it): it gets all the numbers from 0-19 in a random order. That is very different to asking for 20 random numbers between 0-19. Hint: duplicates. – Marc Gravell Jul 03 '14 at 07:16
  • @Alexander: Ah, that makes sense. Interesting technique. Thanks for clarifying! – stakx - no longer contributing Jul 03 '14 at 07:19
  • @MarcGravell does the first return statement produce duplicates with the same instance of `Random`? – Leo Jul 03 '14 at 07:21
  • 1
    @Leo neither produces duplicates, since the source is simply `Enumerable.Range(0,20)`. It doesn't matter how you sort that: it doesn't contain duplicates. – Marc Gravell Jul 03 '14 at 07:22
  • I bottled the hammer, but [this is one of the most common duplicates on here](http://stackoverflow.com/questions/767999/random-number-generator-only-generating-one-random-number). – Rawling Jul 03 '14 at 07:25
  • 1
    Just to be clear, though: When shuffling a sequence you probably want a [shuffle](http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle), not a sorting algorithm. – Joey Jul 03 '14 at 07:27
  • @Joey - Sorting by random is mathematically as good as a shuffle. It might be a little more compute intensive though. – Enigmativity Jul 07 '20 at 10:43
  • @Enigmativity: Yeah, you've got an additional log(n) factor. It's just about the correct choice of algorithm. – Joey Jul 09 '20 at 20:58
  • @Joey - It's also about simpler code and ease of implementation. When the list is relatively small that counts too. – Enigmativity Jul 09 '20 at 23:18

1 Answers1

6

Random class initializes with seed on construction - this seed will be used to produce unique sequence of numbers. In case if seed is not specified - Environment.TickCount is used as seed. In your case, sequence is constructed so fast, that Random class gets same seed in every constructed instance. So all items return same value, and not randomly sorted.

First working line, on the other hand, have single Random class instance for every enumerable item. So calling .Next() produces next random number, although seed is the same, and sequence gets randomly sorted.

Speaking of generating random sequences without duplicates - look at Fisher-Yates shuffle. Your example with Random() has side effects - executing every loop iteration changes internal state of Random() class instance, which can lead to undesired behavior - for example, if sorting algorithm relies on fact that each sequence item returns same number during sort (this is not the case with .net, but who knows - internal implementation may change in future).

Community
  • 1
  • 1
Alexander
  • 4,153
  • 1
  • 24
  • 37
  • "Your example with Random() has side effects" - What side-effects? "can lead to undesired behavior" - what undesired behaviour? – Enigmativity Jan 13 '18 at 23:22