Random numbers (part II)

  • Thread starter Thread starter BOOGIEMAN
  • Start date Start date
B

BOOGIEMAN

//Why this doesn't work :

using System;
using System.Threading;

class RandomObjectDemo
{
public static void Main( )
{
Thread.Sleep( 1 );

int RND01 = randObj.Next( );
double RND02 = randObj.NextDouble( );

Console.WriteLine("RND01 = " + RND01 );
Console.WriteLine("RND02 = " + RND02 );
}
}
----------------------------------
//Example above is my rework from this, which works :

using System;
using System.Threading;

public class RandomObjectDemo
{
static void RunIntNDoubleRandoms( Random randObj )
{
int RND01 = randObj.Next( );
double RND02 = randObj.NextDouble( );

Console.WriteLine("RND01 = " + RND01 );
Console.WriteLine("RND02 = " + RND02 );
}

static void Main( )
{
Thread.Sleep( 1 );

Random autoRand = new Random( );
RunIntNDoubleRandoms( autoRand );
}
}
 
See my notes inline:

BOOGIEMAN said:
//Why this doesn't work :

using System;
using System.Threading;

class RandomObjectDemo
{
public static void Main( )
{
Thread.Sleep( 1 );

int RND01 = randObj.Next( );
double RND02 = randObj.NextDouble( );

Console.WriteLine("RND01 = " + RND01 );
Console.WriteLine("RND02 = " + RND02 );
}
}

Where is your randObj being created in the above? What is the error?

----------------------------------
//Example above is my rework from this, which works :

using System;
using System.Threading;

public class RandomObjectDemo
{
static void RunIntNDoubleRandoms( Random randObj )
{
int RND01 = randObj.Next( );
double RND02 = randObj.NextDouble( );

Console.WriteLine("RND01 = " + RND01 );
Console.WriteLine("RND02 = " + RND02 );
}

static void Main( )
{
Thread.Sleep( 1 );

Random autoRand = new Random( );
RunIntNDoubleRandoms( autoRand );
}
}

To generate a random number between a given range, use Next just like
normal..but give it a range.

ie:
Random r = new Random(Environment.TickCount); // give it a random seed
to help make it more random
int i = r.Next(1, 10); // generate number between 1 and 9

See also:
http://msdn.microsoft.com/library/d...ref/html/frlrfsystemrandomclassnexttopic3.asp

-sb
 
Easy mistake to make :) It seems logicial for someone to assume that the
number would be between 5 and 55...but it isn't. Go figure...

-sb
 
To generate a random number between a given range, use Next just like
normal..but give it a range.

ie:
Random r = new Random(Environment.TickCount); // give it a random seed
to help make it more random
int i = r.Next(1, 10); // generate number between 1 and 9


Thank you so much, I finally can make my own lottery program.
If I win something I'll send you your share ;))

BTW, do I need Thread.Sleep( 1 ); line ?
Does it make more reliable random numbers ?
My code now looks :


using System;
//using System.Threading;

class RandomProba
{
public static void Main( )
{
//Thread.Sleep( 1 );
Random r = new Random(Environment.TickCount);
int mojBroj = r.Next(1, 40);

Console.WriteLine("Moj Broj = " + mojBroj );
}
}


and what does Environment.TickCount stands for, again ?
are there any else Environment.xxxxx or xxxxx.TickCount
commands that I could use ?
 
Thread.Sleep should not affect the number generation at all. All it should
really effectively do is allow Windows to do a context switch...

Actually, after thinking about it, Environment.TickCount isn't needed as
it's already the default seed that is selected if you don't specify a seed
in the constructor. Anyway, you can get more info on it here:
http://msdn.microsoft.com/library/d...frlrfsystemenvironmentclasstickcounttopic.asp

You can review the entire Environment class here:

http://msdn.microsoft.com/library/d...ref/html/frlrfsystemenvironmentclasstopic.asp

Glad I could help
-sb
 
Back
Top