473,320 Members | 2,104 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,320 software developers and data experts.

More on Randomness (with text this time :-)

Hi,
There have been quite a few discussions of Random not giving random
numbers and how to fix that by feeding in a new seed each time, waiting
enough time, or calling the Next() method of the Random object. I have
another wrinkle....
I have created a static Random DNA sequence generator (it is really just
a random number generator with a little chrome). Since it is static, I can't
use the .Next method for multiple calls to the object (or if I can, can you
please let me know how?). Right now, I am have a sister method which allows
the user to pass in a seed, so I can generate THAT seed with a random number
on which I call Next() each time. After discussion from this group, I though
I might add a Thread.Sleep() to the unseeded version and have it wait a tick
each time before running, but a trick turns out not to be long enough! I
found that I have to wait ~80000 ticks before I get a new random number! If
anyone knows of a better way to get this static method to be more truely
random without waiting that long, I would be interested.

Here is the method
public static string RandomSequence (int length)
{
Random Number = new Random();
StringBuilder RandomSequence = new StringBuilder();
int ThisBase; "
for (int Base = 1; Base <= length; Base++)
{
ThisBase = Number.Next(4);
switch (ThisBase)
{
case 0:
RandomSequence.Append("A");
break;
case 1:
RandomSequence.Append("G");
break;
case 2:
RandomSequence.Append("T");
break;
case 3:
RandomSequence.Append("C");
break;
default:
break;
}
}
return RandomSequence.ToString();
}
and here is what I did to determine how many ticks it takes to get a new
number

private void Page_Load(object sender, System.EventArgs e)
{
int NumberNumber = 20;
TimeSpan ATick = new TimeSpan(10000L);
for (int Counter = 0; Counter < NumberNumber;
Counter++)//This returns the same sequence each time
{
RandomNumberLabel.Text += "<br>" +
BiologyTools.NucleicAcidSequence.RandomSequence(10 , 20);
}
RandomNumberLabel.Text += "<p>";
for (int Counter = 0; Counter < NumberNumber;
Counter++)//This changes every 8 passes
{
System.Threading.Thread.Sleep(ATick);
RandomNumberLabel.Text += "<br>" +
BiologyTools.NucleicAcidSequence.RandomSequence(10 , 20);
}
}

Ethan
Jan 17 '07 #1
3 1991
It's all about the seed. there is a site that uses some kind of atmospheric
measurement that is random to generate random numbers and you can link to
that site, get the random numbers and use that for seeding. Or use some kind
of other item during your programmes run life that is random. For example if
its a game where players take turns, the time each will take to make a move
at the ms level will be truly random, there is also a cryptographic api you
can use to generate random numbers, again you could use that for the seed or
mix it with other random numbers to generate a new random seed. The point is
the seed must come from a random source, if your seed doesn't change the
random result will be the same.

I believe the site is www.random.org.

Hope that helps?
"Ethan Strauss" <ethan dot strauss at Promega dot comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
Hi,
There have been quite a few discussions of Random not giving random
numbers and how to fix that by feeding in a new seed each time, waiting
enough time, or calling the Next() method of the Random object. I have
another wrinkle....
I have created a static Random DNA sequence generator (it is really
just a random number generator with a little chrome). Since it is static,
I can't use the .Next method for multiple calls to the object (or if I
can, can you please let me know how?). Right now, I am have a sister
method which allows the user to pass in a seed, so I can generate THAT
seed with a random number on which I call Next() each time. After
discussion from this group, I though I might add a Thread.Sleep() to the
unseeded version and have it wait a tick each time before running, but a
trick turns out not to be long enough! I found that I have to wait ~80000
ticks before I get a new random number! If anyone knows of a better way
to get this static method to be more truely random without waiting that
long, I would be interested.

Here is the method
public static string RandomSequence (int length)
{
Random Number = new Random();
StringBuilder RandomSequence = new StringBuilder();
int ThisBase; "
for (int Base = 1; Base <= length; Base++)
{
ThisBase = Number.Next(4);
switch (ThisBase)
{
case 0:
RandomSequence.Append("A");
break;
case 1:
RandomSequence.Append("G");
break;
case 2:
RandomSequence.Append("T");
break;
case 3:
RandomSequence.Append("C");
break;
default:
break;
}
}
return RandomSequence.ToString();
}
and here is what I did to determine how many ticks it takes to get a new
number

private void Page_Load(object sender, System.EventArgs e)
{
int NumberNumber = 20;
TimeSpan ATick = new TimeSpan(10000L);
for (int Counter = 0; Counter < NumberNumber;
Counter++)//This returns the same sequence each time
{
RandomNumberLabel.Text += "<br>" +
BiologyTools.NucleicAcidSequence.RandomSequence(10 , 20);
}
RandomNumberLabel.Text += "<p>";
for (int Counter = 0; Counter < NumberNumber;
Counter++)//This changes every 8 passes
{
System.Threading.Thread.Sleep(ATick);
RandomNumberLabel.Text += "<br>" +
BiologyTools.NucleicAcidSequence.RandomSequence(10 , 20);
}
}

Ethan

Jan 17 '07 #2
Hi Ethan,

your problem is, that you create a new instance of Random in every call. So
if several calls fall into the same timeslice (obviously about 80000 ticks)
all will get the same sequence of random numbers. You should try to reuse
the same instance for all calls. This will have to be stored in a static
field.

static Random Number = new Random(); //RNG instanciated on class
initialization

The method will be the same, without the first line.
This would not be threadsafe.
You would have to lock any access to the static Instance, for threadsafty.
Even better is using the static Instance for creating a seed for a local
instance. By this you minimize the locking.

static Random _number = new Random(); //RNG instanciated on class
initialization

public static string RandomSequence (int length)
{
lock(_number)
Random Number = new Random(_number.Next(1000));
int ThisBase;
......

HTH

"Ethan Strauss" <ethan dot strauss at Promega dot comschrieb im
Newsbeitrag news:%2****************@TK2MSFTNGP02.phx.gbl...
Hi,
There have been quite a few discussions of Random not giving random
numbers and how to fix that by feeding in a new seed each time, waiting
enough time, or calling the Next() method of the Random object. I have
another wrinkle....
I have created a static Random DNA sequence generator (it is really
just a random number generator with a little chrome). Since it is static,
I can't use the .Next method for multiple calls to the object (or if I
can, can you please let me know how?). Right now, I am have a sister
method which allows the user to pass in a seed, so I can generate THAT
seed with a random number on which I call Next() each time. After
discussion from this group, I though I might add a Thread.Sleep() to the
unseeded version and have it wait a tick each time before running, but a
trick turns out not to be long enough! I found that I have to wait ~80000
ticks before I get a new random number! If anyone knows of a better way
to get this static method to be more truely random without waiting that
long, I would be interested.

Here is the method
public static string RandomSequence (int length)
{
Random Number = new Random();
StringBuilder RandomSequence = new StringBuilder();
int ThisBase; "
for (int Base = 1; Base <= length; Base++)
{
ThisBase = Number.Next(4);
switch (ThisBase)
{
case 0:
RandomSequence.Append("A");
break;
case 1:
RandomSequence.Append("G");
break;
case 2:
RandomSequence.Append("T");
break;
case 3:
RandomSequence.Append("C");
break;
default:
break;
}
}
return RandomSequence.ToString();
}
and here is what I did to determine how many ticks it takes to get a new
number

private void Page_Load(object sender, System.EventArgs e)
{
int NumberNumber = 20;
TimeSpan ATick = new TimeSpan(10000L);
for (int Counter = 0; Counter < NumberNumber;
Counter++)//This returns the same sequence each time
{
RandomNumberLabel.Text += "<br>" +
BiologyTools.NucleicAcidSequence.RandomSequence(10 , 20);
}
RandomNumberLabel.Text += "<p>";
for (int Counter = 0; Counter < NumberNumber;
Counter++)//This changes every 8 passes
{
System.Threading.Thread.Sleep(ATick);
RandomNumberLabel.Text += "<br>" +
BiologyTools.NucleicAcidSequence.RandomSequence(10 , 20);
}
}

Ethan

Jan 17 '07 #3
<"Ethan Strauss" <ethan dot strauss at Promega dot com>wrote:
There have been quite a few discussions of Random not giving random
numbers and how to fix that by feeding in a new seed each time, waiting
enough time, or calling the Next() method of the Random object. I have
another wrinkle....
I have created a static Random DNA sequence generator (it is really just
a random number generator with a little chrome). Since it is static, I can't
use the .Next method for multiple calls to the object (or if I can, can you
please let me know how?).
Just have the Random stored as a static variable:

static Random rng = new Random();

Then use it from your static method.

However, Random isn't thread-safe, so you either need to explicitly
lock on every call, or use my StaticRandom class which basically makes
things simpler by doing the locking for you (and creating the Random,
in fact).

See http://www.pobox.com/~skeet/csharp/miscutil

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 17 '07 #4

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

Similar topics

14
by: Tina Li | last post by:
Hello, I've been struggling with a regular expression for parsing XML files, which keeps giving the run time error "maximum recursion limit exceeded". Here is the pattern string: ...
0
by: Joe Kraft | last post by:
I'm hoping someone can help me out. We're running a website that uses XML/XSLT transformations, VB.Net and an Oracle database. Currently the site cannot support more than 6-7 users at a time...
0
by: Thomas Anderson | last post by:
I have had this database up for several months and suddenly I began getting "Data type mismatch in criteria expression" Errors all over the place. I can open a query and get this error, then say...
2
by: misseill | last post by:
Hi there, I am currently working on converting a program from Access 97 to Access 2003. It all worked fine in 97, but there is some randomness in 2003 that I don't understand. I have a form,...
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...
0
by: robert | last post by:
As more and more python packages are starting to use the bloomy (Java-ish) 'logging' module in a mood of responsibility and as I am not overly happy with the current "thickener" style of usage, I...
8
by: john | last post by:
To test a new piece of software designed to help with (among other things) eCommerce WWW site development. The software is fairly easy to use but you must fit a profile. Retail price is 120 GBP and...
2
by: Ethan Strauss | last post by:
4
by: nier | last post by:
Hey I am trying to figure out a very simple code to do some weighted randomness. I have two values, and I have to randomly pick among them on each iteration. But I need, let's say, the first...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.