By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,857 Members | 1,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,857 IT Pros & Developers. It's quick & easy.

Random too Random

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Marshall Belew wrote:

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?
Nov 16 '05 #2

P: n/a
Yes, mscorlib.dll version 1.1.4322.573.

"Julie" <ju***@nospam.com> wrote in message
news:41***************@nospam.com...
Marshall Belew wrote:

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?

Nov 16 '05 #3

P: n/a
Oddity: I can't really reproduce this when I use a stand alone application.
It seems to only happen in my primary app.
"Marshall Belew" <mb****@koiosworks.com> wrote in message
news:10*************@corp.supernews.com...
Yes, mscorlib.dll version 1.1.4322.573.

"Julie" <ju***@nospam.com> wrote in message
news:41***************@nospam.com...
Marshall Belew wrote:

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?


Nov 16 '05 #4

P: n/a
Can you post the code you are using that generates the output you showed? (For both machine 1 and machine 2)

--
Adam Clauss
ca*****@tamu.edu

"Marshall Belew" <mb****@koiosworks.com> wrote in message news:10*************@corp.supernews.com...
Oddity: I can't really reproduce this when I use a stand alone application.
It seems to only happen in my primary app.
"Marshall Belew" <mb****@koiosworks.com> wrote in message
news:10*************@corp.supernews.com...
Yes, mscorlib.dll version 1.1.4322.573.

"Julie" <ju***@nospam.com> wrote in message
news:41***************@nospam.com...
Marshall Belew wrote:
>
> 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?



Nov 16 '05 #5

P: n/a
Marshall Belew wrote:
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.


Thanks!

Marshall Belew

--
mikeb
Nov 16 '05 #6

P: n/a
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" <ca*****@tamu.edu> wrote in message
news:Og**************@tk2msftngp13.phx.gbl...
Can you post the code you are using that generates the output you showed? (For both machine 1 and machine 2)
--
Adam Clauss
ca*****@tamu.edu

"Marshall Belew" <mb****@koiosworks.com> wrote in message

news:10*************@corp.supernews.com...
Oddity: I can't really reproduce this when I use a stand alone application. It seems to only happen in my primary app.
"Marshall Belew" <mb****@koiosworks.com> wrote in message
news:10*************@corp.supernews.com...
Yes, mscorlib.dll version 1.1.4322.573.

"Julie" <ju***@nospam.com> wrote in message
news:41***************@nospam.com...
> Marshall Belew wrote:
> >
> > 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?



Nov 16 '05 #7

P: n/a

"mikeb" <ma************@nospam.mailnull.com> wrote in message
news:uo**************@TK2MSFTNGP11.phx.gbl...
Marshall Belew wrote:
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.


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.


Thanks!

Marshall Belew

--
mikeb

Nov 16 '05 #8

P: n/a
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

"Marshall Belew" <mb****@koiosworks.com> wrote in message
news:c8*************************@posting.google.co m...
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

Nov 16 '05 #9

P: n/a
"Joris Dobbelsteen" <RE********************@jAoris2k.aTth.cXx> wrote in
message news:41***********************@news.euronet.nl...
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.
- Joris

"Marshall Belew" <mb****@koiosworks.com> wrote in message
news:c8*************************@posting.google.co m...
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


Nov 16 '05 #10

P: n/a
"Marshall Belew" <mb****@koiosworks.com> wrote in message
news:10*************@corp.supernews.com...
I wish I knew more about the internals of the Random class.


Try this:

http://www.dotnet247.com/247referenc...Random/__rotor

Marc
Nov 16 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.