2
Reply

C# RNG - Creating Dice Roller - Unexpected results *Solved*

Mazetar

Mazetar

Mar 31 2011 8:10 AM
4.2k
Hello!
I'm trying to create an simple text based RPG,
for this I need some random nummbers between x and y where x and y varies depending on which dice is needed.
I have multipple dices like D6 (ranging from 1 to 6) and D20(ranging from 1 to 20)
so I decided to use the .Net 's rng and just make functions like D6 and D20 to call for a random nummber between x and y.
but I'm getting some strange output, heres the code, then the outputs.
NB: The code is cut out, this is all the code used for the RNG and dices,
I excluded the rest since it's not relevant in any way
From Dices.cs
static class Dices
{
static public int D6()
{
return randomNummber(1, 6);
}
static public int randomNummber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
And heres the Program.cs code:
int dice = Dices.D6();
Console.WriteLine(dice);
Console.ReadLine();
dice = Dices.D6();
Console.WriteLine(dice);
Console.ReadLine();
This reuslts in the following output example:
1
4
but if I do like this:
dice = Dices.D6();
Console.WriteLine(dice);
int dice = Dices.D6();
Console.WriteLine(dice);
dice = Dices.D6();
Console.WriteLine(dice);
Console.ReadLine();
dice = Dices.D6();
Console.WriteLine(dice);
int dice = Dices.D6();
Console.WriteLine(dice);
dice = Dices.D6();
Console.WriteLine(dice);
Console.ReadLine();
dice = Dices.D6();
Console.WriteLine(dice);
Console.ReadLine();
Using Multipple dice rolls without ReadLine between each results in them being the same:
This would for example output; 1,1,1 4,4,4 ,2
All that is generated after the first one in a row is the same for some reason,
if I use the debug and jump into the code and follow it step by step,
it generates random nummbers anyways:O
anyone has any suggestion to what I can do different?

Answers (2)