473,503 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Random too Random

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
10 2484
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
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
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
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
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
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

"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
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
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
3653
by: Paul Rubin | last post by:
http://www.nightsong.com/phr/python/sharandom.c This is intended to be less predicable/have fewer correlations than the default Mersenne Twister or Wichmann-Hill generators. Comments are...
5
1478
by: Bill | last post by:
Hello, If I type the following code directly into the interpreter, it works. If I run it from a script, it generates the following error. Can someone help? Thanks! ------------------------ ...
4
2736
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
2
2194
by: Brendon Towle | last post by:
I need to simulate scenarios like the following: "You have a deck of 3 orange cards, 5 yellow cards, and 2 blue cards. You draw a card, replace it, and repeat N times." So, I wrote the...
6
2114
by: Pao | last post by:
My code works in this way: I declared a static array in a class (public static int GVetRandom = new int;) that in a for cycle I fill with random numbers. The array gets cleared (clear method) and...
6
4793
by: Mike Langworthy | last post by:
i can not seem to get this code to work. someone please help using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
15
2707
by: caca | last post by:
Hello, This is a question for the best method (in terms of performance only) to choose a random element from a list among those that satisfy a certain property. This is the setting: I need to...
4
11529
by: globalrev | last post by:
do i need to import something to use random?
11
11530
by: Alex | last post by:
Hi everybody, I wonder if it is possible in python to produce random numbers according to a user defined distribution? Unfortunately the random module does not contain the distribution I need...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6982
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.