random number generator help

M

Mike Langworthy

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);
}


}
}
 
A

Alberto Poblacion

Mike Langworthy said:
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);
}
}
}
 
P

Peter Bradley

Chakravarthy said:
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
 
A

atlaste

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);
}
}
}
---

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
 
P

Peter Bradley

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

I never did think that hello world example was really enterprise class.

;-)


Peter
 
A

atlaste

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top