By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,760 Members | 1,557 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,760 IT Pros & Developers. It's quick & easy.

Random Numbers - Will this produce working results?

P: n/a
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[i] =
characters[mt_r1.Next(characters.Length)];
}
// Fill odd numbered indices with randomized character
from the set
else if (i % 2 == 1)
{
ret[i] =
characters[mt_r2.Next(characters.Length)];
}
}

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

// end

Feb 18 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
te********@gmail.com wrote:
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
Feb 18 '07 #2

P: n/a
On Feb 18, 5:21 pm, Arne Vajhj <a...@vajhoej.dkwrote:
teh.sn1...@gmail.com wrote:
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
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.

Feb 18 '07 #3

P: n/a
te********@gmail.com wrote:
On Feb 18, 5:21 pm, Arne Vajhj <a...@vajhoej.dkwrote:
>teh.sn1...@gmail.com wrote:
>>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 ?

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.CryptographyRNGCryptoServiceProvid er
suits your needs better.

Arne

Feb 18 '07 #4

P: n/a
You need to observe significant less values to predict the outcome
from Random class.

Maybe System.Security.CryptographyRNGCryptoServiceProvid er
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

Feb 19 '07 #5

P: n/a
JoeW wrote:
>You need to observe significant less values to predict the outcome
from Random class.

Maybe System.Security.CryptographyRNGCryptoServiceProvid er
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
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.

--
Gran Andersson
_____
http://www.guffa.com
Feb 19 '07 #6

P: n/a
JoeW wrote:
>You need to observe significant less values to predict the outcome
from Random class.

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
Feb 19 '07 #7

P: n/a
Excellent, thats what I wanted to know. Thanks again for the help I
really appreciate it.

Feb 19 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.