473,322 Members | 1,734 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,322 software developers and data experts.

Random Numbers - Will this produce working results?

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
7 2001
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
On Feb 18, 5:21 pm, Arne Vajhøj <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
te********@gmail.com wrote:
On Feb 18, 5:21 pm, Arne Vajhøj <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
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
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.

--
Göran Andersson
_____
http://www.guffa.com
Feb 19 '07 #6
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
Excellent, thats what I wanted to know. Thanks again for the help I
really appreciate it.

Feb 19 '07 #8

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

Similar topics

6
by: Trythat | last post by:
This might seem a stupid question but here goes - I wrote a key generator for one of my programs in Visual Basic, so I recieve sales notification via email and then use my program to generate the...
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...
9
by: Stu Banter | last post by:
Hi, On a previous question someone recommended using the System.Security.Cryptography to fill an array with strong random bytes. It works but I can't specify the max value of course.. I solved...
104
by: fieldfallow | last post by:
Hello all, Is there a function in the standard C library which returns a prime number which is also pseudo-random? Assuming there isn't, as it appears from the docs that I have, is there a...
6
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...
5
by: Muffin | last post by:
I hope somebody can show me why I need to have a messagebox to get "random" numbers in my example. If I comment out the message box call that is in RollAbility() the numbers generated are not...
7
by: bipi | last post by:
Dear all, I found function rand(), it can create random number but this function can not define the range of number which I want to get it, such as, I want to get random number in the range from...
20
by: Robbie Hatley | last post by:
I needed a quick program called "random" that gives a random positive integer from n1 to n2. For example, if I type "random 38, 43", I want the program to print a random member of the set {38,...
16
by: raylopez99 | last post by:
For the public record. RL public void IterateOne() { Random myRandom = new Random(); //declare Random outside the iteration for (int j = 0; j < Max; j++) {
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...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
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: 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.