Connecting Tech Pros Worldwide Forums | Help | Site Map

Random numbers (part II)

BOOGIEMAN
Guest
 
Posts: n/a
#1: Nov 16 '05
//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 );
}
}
----------------------------------
I need simpliest way to generate few random numbers.
Also, how to generate number between (for example) 5 and 55 ?

SB
Guest
 
Posts: n/a
#2: Nov 16 '05

re: Random numbers (part II)


See my notes inline:

"BOOGIEMAN" <BOOGIEMANPN@YAHOO.COM> wrote in message
news:1emy1vku10ogr.598curnetlmh$.dlg@40tude.net...[color=blue]
> //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 );
> }
> }[/color]

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

[color=blue]
> ----------------------------------
> //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 );
> }
> }
> ----------------------------------
> I need simpliest way to generate few random numbers.
> Also, how to generate number between (for example) 5 and 55 ?[/color]

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/de...nexttopic3.asp

-sb


Peter Jausovec
Guest
 
Posts: n/a
#3: Nov 16 '05

re: Random numbers (part II)


Hi,

Use rnd.Next (5,55) to get the random number from 5 to 55.

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"BOOGIEMAN" <BOOGIEMANPN@YAHOO.COM> je napisal v sporocilo
news:1emy1vku10ogr.598curnetlmh$.dlg@40tude.net ...[color=blue]
> //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 );
> }
> }
> ----------------------------------
> I need simpliest way to generate few random numbers.
> Also, how to generate number between (for example) 5 and 55 ?[/color]


SB
Guest
 
Posts: n/a
#4: Nov 16 '05

re: Random numbers (part II)


That will give him a random number between 5 and 54...

-sb

"Peter Jausovec" <peter@jausovec.net> wrote in message
news:ObjEW7xJFHA.2752@TK2MSFTNGP10.phx.gbl...[color=blue]
> Hi,
>
> Use rnd.Next (5,55) to get the random number from 5 to 55.
>
> --
> Regards,
> Peter Jausovec
> (http://blog.jausovec.net)
> "BOOGIEMAN" <BOOGIEMANPN@YAHOO.COM> je napisal v sporocilo
> news:1emy1vku10ogr.598curnetlmh$.dlg@40tude.net ...[color=green]
>> //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 );
>> }
>> }
>> ----------------------------------
>> I need simpliest way to generate few random numbers.
>> Also, how to generate number between (for example) 5 and 55 ?[/color]
>
>[/color]


Peter Jausovec
Guest
 
Posts: n/a
#5: Nov 16 '05

re: Random numbers (part II)


my bad .. ;)

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"SB" <stormfire1@yahoo.com> je napisal v sporočilo
news:%23kJ8g9xJFHA.572@tk2msftngp13.phx.gbl ...[color=blue]
> That will give him a random number between 5 and 54...
>
> -sb
>
> "Peter Jausovec" <peter@jausovec.net> wrote in message
> news:ObjEW7xJFHA.2752@TK2MSFTNGP10.phx.gbl...[color=green]
>> Hi,
>>
>> Use rnd.Next (5,55) to get the random number from 5 to 55.
>>
>> --
>> Regards,
>> Peter Jausovec
>> (http://blog.jausovec.net)
>> "BOOGIEMAN" <BOOGIEMANPN@YAHOO.COM> je napisal v sporocilo
>> news:1emy1vku10ogr.598curnetlmh$.dlg@40tude.net ...[color=darkred]
>>> //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 );
>>> }
>>> }
>>> ----------------------------------
>>> I need simpliest way to generate few random numbers.
>>> Also, how to generate number between (for example) 5 and 55 ?[/color]
>>
>>[/color]
>
>[/color]


SB
Guest
 
Posts: n/a
#6: Nov 16 '05

re: Random numbers (part II)


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


"Peter Jausovec" <peter@jausovec.net> wrote in message
news:eRn5MCyJFHA.3420@tk2msftngp13.phx.gbl...[color=blue]
> my bad .. ;)
>
> --
> Regards,
> Peter Jausovec
> (http://blog.jausovec.net)
> "SB" <stormfire1@yahoo.com> je napisal v sporočilo
> news:%23kJ8g9xJFHA.572@tk2msftngp13.phx.gbl ...[color=green]
>> That will give him a random number between 5 and 54...
>>
>> -sb
>>
>> "Peter Jausovec" <peter@jausovec.net> wrote in message
>> news:ObjEW7xJFHA.2752@TK2MSFTNGP10.phx.gbl...[color=darkred]
>>> Hi,
>>>
>>> Use rnd.Next (5,55) to get the random number from 5 to 55.
>>>
>>> --
>>> Regards,
>>> Peter Jausovec
>>> (http://blog.jausovec.net)
>>> "BOOGIEMAN" <BOOGIEMANPN@YAHOO.COM> je napisal v sporocilo
>>> news:1emy1vku10ogr.598curnetlmh$.dlg@40tude.net ...
>>>> //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 );
>>>> }
>>>> }
>>>> ----------------------------------
>>>> I need simpliest way to generate few random numbers.
>>>> Also, how to generate number between (for example) 5 and 55 ?
>>>
>>>[/color]
>>
>>[/color]
>
>[/color]


BOOGIEMAN
Guest
 
Posts: n/a
#7: Nov 16 '05

re: Random numbers (part II)


On Sat, 12 Mar 2005 11:17:26 -0500, SB wrote:
[color=blue]
> 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[/color]


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 ?
SB
Guest
 
Posts: n/a
#8: Nov 16 '05

re: Random numbers (part II)


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/de...counttopic.asp

You can review the entire Environment class here:

http://msdn.microsoft.com/library/de...classtopic.asp

Glad I could help
-sb

"BOOGIEMAN" <BOOGIEMANPN@YAHOO.COM> wrote in message
news:pwveax770p1a$.13bvtwwu46dwd$.dlg@40tude.net.. .[color=blue]
> On Sat, 12 Mar 2005 11:17:26 -0500, SB wrote:
>[color=green]
>> 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[/color]
>
>
> 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 ?[/color]


Closed Thread