Random Numbers - Will this produce working results?

T

teh.sn1tch

I created a random number generator for an application that uses a
MersenneTwister class I found on the net. Basically I generate two
random numbers from the MersenneTwister class and use each one as a
seed for the built in Random class of the .net framework. To better
illustrate I am prodviding the complete code for this application, it
isn't too much but I would like to know if this is a viable solution
because I am a pretty new to C# and programming so please any negative
or positive criticism is welcome.

// begin

public sealed class Program
{
static Random mt_r1;
static Random mt_r2;
static readonly string spChars =
@"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()<>,.?/|\{}[]+=_-
abcdefghijklmnopqrstuvwxyz";
const string intNums = @"0123456789";
const string path = @"C:\";
const string fileName = @"rng.txt";

// Length of the number generated
const int rLen = 20;

// How many random numbers to generate
const int rNum = 20;

static void Main(string[] args)
{
try
{
// Create an instance of the MersenneTwister class
MersenneTwister randGen = new MersenneTwister();

// Create a new instance of FileStream to create the
flat file for writing
FileStream fs = new FileStream(path + fileName,
FileMode.Create);

// StreamWriter class to write the file stream
StreamWriter sw = new StreamWriter(fs);

// Append header information to the top of the file
sw.WriteLine("id,random_num");

for (int i = 0; i < rNum; ++i)
{
// Create an instance of the Random class using
the
// MersenneTwister value as the seed and assign
the value
mt_r1 = new Random(randGen.Next());

// Create another instance of the Random class
// with a new MersenneTwister value as the seed
and assign
mt_r2 = new Random(randGen.Next());

// Write an id number and random string to file
sw.WriteLine(i + "," + Randomize(spChars, rLen));
}

// Close the stream writer
sw.Close();
}
catch
{

}
}

/// <summary>
/// Randomizes a number set using the Random class
/// and the MersenneTwister algorithm
/// </summary>
/// <param name="characters">Character set.</param>
/// <param name="length">Length of the random string</param>
public static string Randomize(string characters, int length)
{
// Create a char array with a size of the given length
char[] ret = new char[length];

// iterate through the array
for (int i = 0; i < length; i++)
{
// If 'i' is an even number, fill the corresponding
index
// with a value from the randomized character set
if (i % 2 == 0)
{
ret =
characters[mt_r1.Next(characters.Length)];
}
// Fill odd numbered indices with randomized character
from the set
else if (i % 2 == 1)
{
ret =
characters[mt_r2.Next(characters.Length)];
}
}

// Return the new randomized string
return new string(ret);
}
}

// end
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

I created a random number generator for an application that uses a
MersenneTwister class I found on the net. Basically I generate two
random numbers from the MersenneTwister class and use each one as a
seed for the built in Random class of the .net framework.

Why not just use the MT for generating the actual random numbers ?

Arne
 
T

teh.sn1tch

Why not just use the MT for generating the actual random numbers ?

Arne

Well I thought about that, but from what I read (correct me if I am
wrong) the MT algorithm had this said about it "Observing a sufficient
number of iterates (624 in the case of MT19937) allows one to predict
all future iterates." and because of that it suggested using a 2nd
method to add more randomness so to speak.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

Well I thought about that, but from what I read (correct me if I am
wrong) the MT algorithm had this said about it "Observing a sufficient
number of iterates (624 in the case of MT19937) allows one to predict
all future iterates." and because of that it suggested using a 2nd
method to add more randomness so to speak.

You need to observe significant less values to predict the outcome
from Random class.

Maybe System.Security.CryptographyRNGCryptoServiceProvider
suits your needs better.

Arne
 
J

JoeW

You need to observe significant less values to predict the outcome
from Random class.

Maybe System.Security.CryptographyRNGCryptoServiceProvider
suits your needs better.

Arne

Even if the seed for the Random class is from the random number
generated from the MT class? What I am trying to understand is that by
doing this is the .net Random class using a different MT number each
time its used or is it only using it the first time? Ultimately what I
am trying to do is just randomize the MT generated number a little
more, if its not necessary then I will not use it.

Thanks again for helping!
Joe
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

JoeW said:
Even if the seed for the Random class is from the random number
generated from the MT class? What I am trying to understand is that by
doing this is the .net Random class using a different MT number each
time its used or is it only using it the first time? Ultimately what I
am trying to do is just randomize the MT generated number a little
more, if its not necessary then I will not use it.

Thanks again for helping!
Joe

The seed is only used once. The next random number will be seeded from
the previous.

If you need really high quality randomness, use the random generator in
the crypto namespace. It's used for creating encryption keys, but is
rarely needed for anything else.

For any regular use of random numbers, the Random class works just fine.
 
?

=?ISO-8859-1?Q?Arne_Vajh=F8j?=

JoeW said:
Even if the seed for the Random class is from the random number
generated from the MT class?

Where you get the initial seed from should not effect whether
it is possible to identify where in the cycle the algorithm is.
Indeed the MT's very long cycle should make it one of the
more difficult to predict that way.
What I am trying to understand is that by
doing this is the .net Random class using a different MT number each
time its used or is it only using it the first time? Ultimately what I
am trying to do is just randomize the MT generated number a little
more, if its not necessary then I will not use it.

There are no reason to believe that combining MT and
ordinary Random should have any benefits.

Arne
 
J

JoeW

Excellent, thats what I wanted to know. Thanks again for the help I
really appreciate it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top