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

random number generator help

P: n/a
i can not seem to get this code to work. someone please help

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int returnValue = RandomNumber(5, 20);
Console.Write(returnValue);
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
}
}

Apr 27 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Mike Langworthy" <mc*****@comcast.netwrote in message
news:ev**************@TK2MSFTNGP04.phx.gbl...
>i can not seem to get this code to work. someone please help
You are calling an instance method from a static method. It will work if
you add the keyword "static" to the instance method:
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int returnValue = RandomNumber(5, 20);
Console.Write(returnValue);
}
private static int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
}
}

Apr 27 '07 #2

P: n/a
Change the method to STATIC instead of PRIVATE, then it will work

--
Every thing is perfect, as long as you share!!!
"Mike Langworthy" wrote:
i can not seem to get this code to work. someone please help

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int returnValue = RandomNumber(5, 20);
Console.Write(returnValue);
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
}
}

Apr 27 '07 #3

P: n/a
"Chakravarthy" <ds******@india.comwrote in message
news:DC**********************************@microsof t.com...
Change the method to STATIC instead of PRIVATE, then it will work

--
Better still, join the world of objects and get out of static mode. I know
it's overkill for this project, but it's good practise for the future IMHO.
Something like:

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Program Random = new Program()
int returnValue = Random.RandomNumber(5, 20);
Console.WriteLine(returnValue);
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
}
}
Peter
Apr 27 '07 #4

P: n/a
While we're at it, put the Random variable as a private member in the
class so that it won't keep on generating the same number within a
second (iirc the random is seeded with the time value in seconds!)

Oh and for convenience the class that does the random thingie
shouldn't be Program but MyCoolRandomNess or something like that. And
mixing uppercase variables and uppercase class names is a bad idea as
well, so we change "Random" to "random".

And once that happens you can wonder what's the use of the class and
the "RandomNumber" method in the first place, since it does exactly
the same as the build-in Random class...

Yes, everyday considerations reflected to a hello world example. :-)

Cheers,
Stefan.

--- Step 1
namespace ConsoleApplication1
{
class Program
{
private Random random = new Random();

static void Main(string[] args)
{
Program Random = new Program()
int returnValue = Random.RandomNumber(5, 20);
Console.WriteLine(returnValue);
}
private int RandomNumber(int min, int max)
{
return random.Next(min, max);
}
}
}
---

--- Step 2
namespace ConsoleApplication1
{
class Program
{

static void Main(string[] args)
{
MyCoolRandomnessClass random = new
MyCoolRandomnessClass();
int returnValue = random.RandomNumber(5, 20);
Console.WriteLine(returnValue);
}
}
public class MyCoolRandomnessClass
{
private Random random = new Random();

public int RandomNumber(int min, int max)
{
return random.Next(min, max);
}
}
}
---

--- Step 3
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Random random = new Random();
int returnValue = random.Next(5, 20);
Console.WriteLine(returnValue);
}
}
}
---

On 27 apr, 08:58, "Peter Bradley" <pbrad...@uwic.ac.ukwrote:
"Chakravarthy" <dskch...@india.comwrote in message

news:DC**********************************@microsof t.com...
Change the method to STATIC instead of PRIVATE, then it will work
--

Better still, join the world of objects and get out of static mode. I know
it's overkill for this project, but it's good practise for the future IMHO.
Something like:

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Program Random = new Program()
int returnValue = Random.RandomNumber(5, 20);
Console.WriteLine(returnValue);
}
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
}

}

Peter


Apr 27 '07 #5

P: n/a
"atlaste" <at*****@gmail.comwrote in message
news:11**********************@n35g2000prd.googlegr oups.com...
Yes, everyday considerations reflected to a hello world example. :-)
I never did think that hello world example was really enterprise class.

;-)
Peter
Apr 27 '07 #6

P: n/a
I never did think that hello world example was really enterprise class.

OT: About a year ago I actually found a couple of random number
generator initialization bugs in some real-life simulation projects.
Most people don't realize it's default initialized with the current
time - which becomes a problem the moment your simulation is running
faster than a single clock tick.

If you are using a framework, I would consider writing a wrapper
around the random class that simply waits a full second. Not because
that's the best solution for the problem (by far), but because most
programmers are performance freaks and will eventually figure out
what's causing the delay and start wondering why that stupid delay is
there -- automatically leading to the correct solution :-)

Cheers,
Stefan.

Apr 27 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.