Random too Random

M

Marshall Belew

I'm trying to synchronize a network app that uses random numbers
generated by System.Random. Rather than pass every randomly generated
number, I just pass the seed. I'm seeing a result that leads me to
believe that a seeded random number is still slightly random. I need
a predictable random number.

Here's my results

Machine 1
Seed: 1453549276

random.Next() = 1997009408
random.Next() = 2105130240
random.Next() = 557073472

Machine 2
Seed: 1453549276

random.Next() = 1997009351
random.Next() = 2105130259
random.Next() = 557073440

You can see that the last few digits of the random numbers are
slightly off. Anyone have any suggestions on how I can generate
predictable random numbers in C#?

Thanks!

Marshall Belew
 
J

Julie

Marshall said:
I'm trying to synchronize a network app that uses random numbers
generated by System.Random. Rather than pass every randomly generated
number, I just pass the seed. I'm seeing a result that leads me to
believe that a seeded random number is still slightly random. I need
a predictable random number.

Here's my results

Machine 1
Seed: 1453549276

random.Next() = 1997009408
random.Next() = 2105130240
random.Next() = 557073472

Machine 2
Seed: 1453549276

random.Next() = 1997009351
random.Next() = 2105130259
random.Next() = 557073440

You can see that the last few digits of the random numbers are
slightly off. Anyone have any suggestions on how I can generate
predictable random numbers in C#?

Thanks!

Marshall Belew

Does each machine have the same version of the .Net framework installed?
 
M

Marshall Belew

Oddity: I can't really reproduce this when I use a stand alone application.
It seems to only happen in my primary app.
 
A

Adam Clauss

Can you post the code you are using that generates the output you showed? (For both machine 1 and machine 2)
 
M

mikeb

Marshall said:
I'm trying to synchronize a network app that uses random numbers
generated by System.Random. Rather than pass every randomly generated
number, I just pass the seed. I'm seeing a result that leads me to
believe that a seeded random number is still slightly random. I need
a predictable random number.

Here's my results

Machine 1
Seed: 1453549276

random.Next() = 1997009408
random.Next() = 2105130240
random.Next() = 557073472

Machine 2
Seed: 1453549276

random.Next() = 1997009351
random.Next() = 2105130259
random.Next() = 557073440

You can see that the last few digits of the random numbers are
slightly off. Anyone have any suggestions on how I can generate
predictable random numbers in C#?

Is one system running the app under a debugger or running a debug build?

The Next() method uses floating point calculations - I seem to recall
that there are some differences in float calculations sometimes seen
between debug and release runs.
 
M

Marshall Belew

Here's the code I'm trying to synchronize. Both computers use this exact
method.

RandomNumberSeed is an integer property

///
public int RandomNumberSeed
{
get { return randomNumberSeed; }
set { randomNumberSeed = value;}
}


BreakUpRandomSeeds is a method I use for generating new random seeds later
on.
Notice I'm printing out the results as soon as they are generated. The
property is merely a placeholder for the data.

///
private void BreakUpRandomSeeds(Unit unit, int seed)
{
Random rnd = new Random(seed);

unit.a.RandomNumberSeed = rnd.Next();
unit.b.RandomNumberSeed = rnd.Next();
unit.c.RandomNumberSeed = rnd.Next();

LogFile.Log("BreakUp: " +
unit.Delta.Name + ": " +
seed + " " +
unit.a.RandomNumberSeed + " " +
unit.b.RandomNumberSeed + " " +
unit.c.RandomNumberSeed);
}


For now, I changed the code a bit. Instead of grabbing .Next(), I use
..Next(1000), which may be just random enough.
So far, I haven't seen differences on both machines. I'm worried that
whatever was causing the slight error I was seeing for the larger
numbers may once in a while cause a rounding difference with the 0 to 1000
set. So far, this hasn't been the case.
I wish I knew more about the internals of the Random class.





Adam Clauss said:
Can you post the code you are using that generates the output you showed?
(For both machine 1 and machine 2)
 
S

Stu Smith

mikeb said:
Is one system running the app under a debugger or running a debug build?

The Next() method uses floating point calculations - I seem to recall
that there are some differences in float calculations sometimes seen
between debug and release runs.

Yep, or maybe even between different processors (eg AMD vs Intel).

Basically, if the OP needs to rely on a sequence, he needs to roll his own
sequence generator.
 
J

Joris Dobbelsteen

Build your own RNG (or port it to .NET).

There are some good documents on how to do this.
What type of performance do you expect from the RNG?

- Joris
 
J

Joris Dobbelsteen

Joris Dobbelsteen said:
Build your own RNG (or port it to .NET).

There are some good documents on how to do this.
What type of performance do you expect from the RNG?

I mean, how good (what kind of distribution) must the RNG be?
Do you need it for security or something different?

Some links:
http://www.math.utah.edu/~alfeld/Random/Random.html
(basic info)

http://www.connotech.com/BBSindex.HTM
x = sqrt(x) mod N

Another way might be to actually use a cipher and do the work. These are
guarenteed to provide the correct values, but are slower.
Benchmark a few to get the performance. I believe RC4 would be quite fast
and it repeats only faster a very very long time.

In order to do synchronization I would use sequential numbers, since they
will never be duplicate and can provide you easier with more information
when running out of sync.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top