473,378 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Random NOt random?



I do the following in code

int count = randata.ProductTable.Rows.Count;

Random r = new Random();

Random r2 = new Random();

int i = r.Next(1,count);

int x = r2.Next(1,count);

and i and x will always be the same....

how does that work?
Nov 18 '05 #1
3 1186
Have you run r.Random() and r2.Random before?

Certainly you have to initialize random number generator before use it.

Note that if you want to use Random() function instead of the object, you
still need to run Randomize statement first.

Regards,
Lau Lei Cheong

"Darren Clark" <dc******@hotmail.com> ¦b¶l¥ó
news:%2****************@TK2MSFTNGP09.phx.gbl ¤¤¼¶¼g...


I do the following in code

int count = randata.ProductTable.Rows.Count;

Random r = new Random();

Random r2 = new Random();

int i = r.Next(1,count);

int x = r2.Next(1,count);

and i and x will always be the same....

how does that work?

Nov 18 '05 #2
I read somewhere that Random is only random if a subsequent call is a
certain amount of time after. Something to do with it being linked to the
clock cycle of which the lowest granularity is ms and as you can do both
calls in one cycle you get teh same number. Put a Sleep() inbetween to
force a context switch and see what happens then.

MattC
"Darren Clark" <dc******@hotmail.com> wrote in message
news:#4**************@TK2MSFTNGP09.phx.gbl...


I do the following in code

int count = randata.ProductTable.Rows.Count;

Random r = new Random();

Random r2 = new Random();

int i = r.Next(1,count);

int x = r2.Next(1,count);

and i and x will always be the same....

how does that work?

Nov 18 '05 #3
Darren Clark wrote:
I do the following in code

int count = randata.ProductTable.Rows.Count;

Random r = new Random();

Random r2 = new Random();

int i = r.Next(1,count);

int x = r2.Next(1,count);

and i and x will always be the same....


The Random class's generator depends entirely on the seed used to
initialize it - 2 generator instance initialized with the same seed will
produce the exact same sequence. At times this is what you want (for
example, when testing you might want to be able to reproduce results).

The Random() constructor you're calling uses the system clock as a seed.
Since the 2 calls are so close together in time it's very, very likely
that they will have the same seed.

See the docs for Random( int) for information on how to avoid this problem.

Also, the Random class is not a particularly good RNG - it's good enough
maybe for games and simple simulations, but if you want a really good
set of random bits look at the RNGCryptoServiceProvider class.

--
mikeb
Nov 18 '05 #4

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

Similar topics

28
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...
6
by: Acacia | last post by:
How would you generate a random number in C++?
10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
10
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
10
by: Marshall Belew | last post by:
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...
15
by: Steven Macintyre | last post by:
Hi all, I need to retrieve an integer from within a range ... this works ... below is my out puts ... it just does not seem so random ... Is there perhaps a suggestion out there to create a...
4
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. ...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I generate a random integer from 1 to N?...
8
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new...
6
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 {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.