473,703 Members | 2,385 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 2503
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.c om> 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****@koioswo rks.com> wrote in message
news:10******** *****@corp.supe rnews.com...
Yes, mscorlib.dll version 1.1.4322.573.

"Julie" <ju***@nospam.c om> 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.ed u

"Marshall Belew" <mb****@koioswo rks.com> wrote in message news:10******** *****@corp.supe rnews.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****@koioswo rks.com> wrote in message
news:10******** *****@corp.supe rnews.com...
Yes, mscorlib.dll version 1.1.4322.573.

"Julie" <ju***@nospam.c om> 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.

RandomNumberSee d is an integer property

///
public int RandomNumberSee d
{
get { return randomNumberSee d; }
set { randomNumberSee d = value;}
}
BreakUpRandomSe eds 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 BreakUpRandomSe eds(Unit unit, int seed)
{
Random rnd = new Random(seed);

unit.a.RandomNu mberSeed = rnd.Next();
unit.b.RandomNu mberSeed = rnd.Next();
unit.c.RandomNu mberSeed = rnd.Next();

LogFile.Log("Br eakUp: " +
unit.Delta.Name + ": " +
seed + " " +
unit.a.RandomNu mberSeed + " " +
unit.b.RandomNu mberSeed + " " +
unit.c.RandomNu mberSeed);
}
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.e du> wrote in message
news:Og******** ******@tk2msftn gp13.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.ed u

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

news:10******** *****@corp.supe rnews.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****@koioswo rks.com> wrote in message
news:10******** *****@corp.supe rnews.com...
Yes, mscorlib.dll version 1.1.4322.573.

"Julie" <ju***@nospam.c om> 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.mailnul l.com> wrote in message
news:uo******** ******@TK2MSFTN GP11.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****@koioswo rks.com> wrote in message
news:c8******** *************** **@posting.goog le.com...
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************ ********@jAoris 2k.aTth.cXx> wrote in
message news:41******** *************** @news.euronet.n l...
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****@koioswo rks.com> wrote in message
news:c8******** *************** **@posting.goog le.com...
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
3693
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 appreciated.
5
1484
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! ------------------------ import random for i in range(10):
4
2752
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. It should put a blue and yellow box on the page with "This is a test" as part of the picture. But what I get is a broken Gif. The other problem is that I can't view the source???? The view source is disabled for this page. What causes this?
2
2205
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 following code, which works, but it seems quite slow to me. Can anyone point out some obvious thing that I'm doing inefficiently? Or, is this more or less as good as it gets? For reference, my typical numbers look like this:
6
2124
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 refilled at each turn of cycle. On my developing pc, the same sequence of random numbers was repeated from on turn of cycle to the other; I put some Application.DoEvents() and all gone well.
6
4812
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
2728
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 pick from a list a random element that satisfies a given property. All or none of the elements may have the property. Most of the time, many of the elements will satisfy the property, and the property is a bit expensive to evaluate. Chance of...
4
11546
by: globalrev | last post by:
do i need to import something to use random?
11
11561
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 :-( Many thanks axel
0
8757
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8669
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9251
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9016
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7864
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6588
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4430
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2447
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.