Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Random(int seed) guarantee?

Tags:

c#

.net

random

I'm working on a project, that relies assigning users random (nothing fancy, just uniformly) subsets of a larger set. Each user has a unique identifier from a set isomorphic to integers. There are two approaches to doing this, as far as I can see.

  1. Create a database junction table between users and keyed elements of the aforementioned larger set with some function once for each user. This can be somewhat impractical for my needs, so I would rather do...
  2. At run-time determine the subset by a similar function but use the unique user id as seed value, and just have the set in memory. Next time it's needed it's created again, from a larger set.

So my question is, if I use the .NET Random object to create the second function using user-id as a seed value, does Microsoft guarantee not to change the Random algorithm in the future? I.e. will all new Random(n)'s Next() sequences be the same forever on all machines?

Alternatively I could create my own random generator, and package it with my code. In fact, this is what I'll probably do, but I'm still curious to know the answer.

like image 676
Gleno Avatar asked Jun 18 '11 05:06

Gleno


1 Answers

No, it is explicitly not guaranteed to be compatible across versions:

The implementation of the random number generator in the Random class is not guaranteed to remain the same across major versions of the .NET Framework. As a result, your application code should not assume that the same seed will result in the same pseudo-random sequence in different versions of the .NET Framework.

like image 81
Mark Brackett Avatar answered Sep 28 '22 04:09

Mark Brackett