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

Random numbers (part II)

//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 ?
Nov 16 '05 #1
7 1611
SB
See my notes inline:

"BOOGIEMAN" <BO*********@YAHOO.COM> wrote in message
news:1e*****************************@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 );
}
}
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 );
}
}
----------------------------------
I need simpliest way to generate few random numbers.
Also, how to generate number between (for example) 5 and 55 ?


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
Nov 16 '05 #2
Hi,

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

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"BOOGIEMAN" <BO*********@YAHOO.COM> je napisal v sporocilo
news:1e*****************************@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 ?

Nov 16 '05 #3
SB
That will give him a random number between 5 and 54...

-sb

"Peter Jausovec" <pe***@jausovec.net> wrote in message
news:Ob**************@TK2MSFTNGP10.phx.gbl...
Hi,

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

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"BOOGIEMAN" <BO*********@YAHOO.COM> je napisal v sporocilo
news:1e*****************************@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 ?


Nov 16 '05 #4
my bad .. ;)

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"SB" <st********@yahoo.com> je napisal v sporočilo
news:%2***************@tk2msftngp13.phx.gbl ...
That will give him a random number between 5 and 54...

-sb

"Peter Jausovec" <pe***@jausovec.net> wrote in message
news:Ob**************@TK2MSFTNGP10.phx.gbl...
Hi,

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

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"BOOGIEMAN" <BO*********@YAHOO.COM> je napisal v sporocilo
news:1e*****************************@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 ?



Nov 16 '05 #5
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
"Peter Jausovec" <pe***@jausovec.net> wrote in message
news:eR**************@tk2msftngp13.phx.gbl...
my bad .. ;)

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"SB" <st********@yahoo.com> je napisal v sporočilo
news:%2***************@tk2msftngp13.phx.gbl ...
That will give him a random number between 5 and 54...

-sb

"Peter Jausovec" <pe***@jausovec.net> wrote in message
news:Ob**************@TK2MSFTNGP10.phx.gbl...
Hi,

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

--
Regards,
Peter Jausovec
(http://blog.jausovec.net)
"BOOGIEMAN" <BO*********@YAHOO.COM> je napisal v sporocilo
news:1e*****************************@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 ?



Nov 16 '05 #6
On Sat, 12 Mar 2005 11:17:26 -0500, SB wrote:
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 ?
Nov 16 '05 #7
SB
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" <BO*********@YAHOO.COM> wrote in message
news:pw******************************@40tude.net.. .
On Sat, 12 Mar 2005 11:17:26 -0500, SB wrote:
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 ?

Nov 16 '05 #8

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

Similar topics

8
by: Aaron | last post by:
I need some help writing this function the function returns a list of random non-duplicate integers in a string, 1 parameter indicating the maximum range. string randGen(int maxRange) ...
13
by: Jon Agiato | last post by:
Hello, I am sure this problem is easy to spot but I have been at this project all day and the frustration has finally overcome me. I am using this function in order to produce a standard normal...
11
by: Mark Rae | last post by:
Hi, My R&D department has asked me to look at threading in a Web Service written in C#, so I came up with the following code: using System; using System.ComponentModel; using...
23
by: Alvin | last post by:
Well, I'm developing a Tetris game in SDL, but when it comes to deciding the next block, I'm stuck. It's random, but when I try something like seeding the randomizer with the time, it won't update...
13
by: porterboy76 | last post by:
If you only use a 32 bit seed for a random number generator, does that mean you can only ever produce a maximum of 2^32 (approx 4 billion) different sequences? What about the Mersenne Twister,...
3
by: clsmith66 | last post by:
I'm hoping someone can help me with a problem I'm having using the random class. I need to return a "random" number between 0 and 15, so i did this Dim r as new Random() Return r.Next(0, 15) ...
14
Deathwing
by: Deathwing | last post by:
Hi everyone, I'm fairly new with python and as such am playing around alot with just basic coding/scripting. I've spent the better part of today working with random numbers. Thanks for all your...
12
by: Naya | last post by:
Hi. I am working on a math tutoring program which generates two random numbers (from 1 to 500) and asks the user to add them. How can I check to see whether or not they put in the correct total??...
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...
26
by: bilgekhan | last post by:
What is the correct method for generating 2 independent random numbers? They will be compared whether they are equal. What about this method: srand(time(0)); int r1 = rand(); srand(rand());...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.