0

Im trying to create a class that will return some random numbers. Now i know how to create a bunch of random numbers and put them in a listbox using :

Random RndNmbrs = new Random();
int n = RndNmbrs.Next();
for (int a = 0; a < 10; a++)
{
    lstBubbleUnorderd.Items.Add(RndNmbrs.Next(0, 10));
}

But I want to put that code in a class and call it from Program.cs, I have tried several things but I cant get it to work so please tell me how one is supposed to do this ?

Adween
  • 2,792
  • 2
  • 18
  • 20

4 Answers4

1

The simple class to generate ranod numbers:

Generator class:

public class RandomNumberGenerator
{
    private Random _rndNmbrs = new Random();

    // Generate's a single random value
    public int Generate(int min, int max)
    {
        return _rndNmbrs.Next(min, max);
    }

    // Generate's a list of random values
    public List<int> Generate(int count, int min, int max)
    {
        var ret = new List<int>();
        for (var i = 0; i < count; i++) 
        {
            ret.Add(_rndNmbrs.Next(min, max);
        }
        return ret;
    }
}

Using:

var gen = new RandomNumberGenerator();
lstBubbleUnorderd.Items.AddRange(gen.Generate(10));
alexmac
  • 19,087
  • 7
  • 58
  • 69
0

You just need to hold the Random instance in the class as instance variable:

public class RandomGenerator
{ 
    private Random _rnd = new Random();

    public int Next(int min, int max)
    {
        return _rnd.Next(min, max);
    }
    // more methods...
}

You can use it for example in this way:

var generator = new RandomGenerator();
List<int> randomNumbers = Enumerable.Range(1, 10)
    .Select(i => generator.Next(0, 10)).ToList();
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
0

I suggest you to made this class as static. Then you don't need instantiate this class before using (because instantiating of this class does'n change it's behavior in any context).

public static class RandomNumberGenerator
{
    private static Random _randomNumberGenerator = new Random();

    // Generate's your random number
    public static int Generate()
    {
        return _rndNmbrs.Next();
    }
}

Then in code you can simply call

int RandomNumber = RandomNumberGenerator.Generate();

without creating new object.

Jaroslav Kadlec
  • 2,505
  • 4
  • 32
  • 43
  • 1
    May or may not be desired. Instead of a static class you could reuse the same `RandomNumberGenerator` instance with the same effect. http://stackoverflow.com/a/4933838/284240 and (acc. thread safety) http://stackoverflow.com/a/4933863/284240 – Tim Schmelter Feb 04 '14 at 10:35
  • +1 thanks for pointing on thread safety. I didn't knew that. – Jaroslav Kadlec Feb 04 '14 at 11:12
0

In the end i did it like this :

private static Random rnd = new Random();
    public static int Generate(int start, int end)
    {
        return rnd.Next(start, end);
    }

and then i call it with a for loop like so :

for (int a = 0; a < 10; a++)
        lstBubbleUnorderd.Items.Add(NummersMaken.Generate(1,100));