last day (15 days later) » 

11:23 AM
1
A: Generating BIGINT Keys using Random Class C#

AlexDevYou have to cast the number to a long before shifting, otherwise this will just shift out the significant bits. tl.ID = ((long)this.r.Next() << 32) | this.r.Next(); Just keep in mind that this method isn't perfect, as pointed out in the question mentioned in the comments, since Random.Next() o...

 
This method can only generate 2^32 many ids, so you might as well cast 1 int to long
 
Why 2^32? I was going to point out now that it can generate 2^62 unique ids because r.Next only returns positive values.
 
Random is deterministic. There are 2^32 possible from the first call, but the second call is entirely predictable and will always return the same value following the first. I.e so if the first call to Next() produces 1589351479 the next Next() call is always 636837953
 
@weston That's the first time I hear that. Do you have any sources for that? Is this flaw inherent to Random.Next() or would it apply also to a solution using Random.NextBytes?
 
Try it: var rand = new Random (123); Assert.AreEqual (1589351479, rand.Next()); Assert.AreEqual (636837953, rand.Next());
And yes it would apply to a nextbytes solution. It's because the seed for the next item is based on the last random value.
 
11:23 AM
Well obviously if you use a fixed seed you are always going to get the same sequence, then you get 2^0, not 2^32. What you stated in the first comment implied that the Random.Next function is periodical, which is something completely different.
 
It's not about the fixed seed, that was to show it is deterministic. Another way to think about it: There are only 2^32 states the Random class can be in before your method is called. Therefore there can only be 2^32 possible outcomes.
No new random entropy is introduced. It relies entirely upon a 32bit seed which is updated (in a predictable way) each time it generates a random value.
 
run this on linqpad. You'll see that 1796769800 appears twice, once followed by 321286284 and once followed by 1265044085.var rand = new Random (123); var used = new HashSet<int>(); while(true) { int a = rand.Next(); a.Dump(); if(used.Contains(a)) break; used.Add(a); } rand.Next().Dump();
 
What does that prove/show?
 
It show that "There are 2^32 possible from the first call, but the second call is entirely predictable and will always return the same value following the first" does not happen, at least not in my currently installed framework. So from a pseudo-random perspective, you get 64 bits of randomness. If you are talking about real, cryptographic randomness, you actually have much less than 32 bits of entropy.
 
All that shows is that if you put in a set you will hit a duplicate eventually.
 
11:25 AM
I think I'm misunderstanding what you meant when you said "There are 2^32 possible from the first call, but the second call is entirely predictable and will always return the same value following the first". Can you explain it better?
 
OK, the random sequence that Random produces is reproducible. You can see that if you set a particular seed, you will always see the same sequence.
But we don't know the seed.
But we do know it is 32 bits.
So the Random class is running though one of 2^32 sequences.
These sequences are all predictable.
So suppose this random sequence is smaller, just 2 bits, and the numbers in the sequence 1 2 3 4. We're taking two at a time, but I can see that there are still only 4 possible outcomes. 12, 23, 34, 41.
There is only one random sequence in Random, seed is just the starting point for it. So imagine a single sequence of 2^32 integers. It again doesn't matter where we start or how we take them, we can only take a maximum of 2^32 unique pairs of ints.
 
But the sequence is not finite and won't repeat after the last item is reached
 
Yes it is finite. And it does repeat.
Hang on, I might need to understand that myself...
I got to get back to work, but I'll take a closer look, what's interesting is the 56 integer seed array...
 
I'll have to look at that later..
But even if it does repeat, it doesn't mean that every time you get x the next number will be y. The linqpad snippet proves that. The first time 1796769800 appears, the next number is 321286284. The second time it appears, it is followed by 1265044085.
 

  last day (15 days later) »