I am currently working on one of my first projects in C#. Right now i want to create something like a lottomachine. It should output 6 numbers all different from each other in the range of 1-49.
private void random()
{
Random rnd = new Random();
z1 = rnd.Next(1, 49);
z2 = rnd.Next(1, 49);
if (z1 == z2)
{
z2 = rnd.Next(1, 49);
}
z3 = rnd.Next(1, 49);
if ((z1 == z3) || (z2 == z3))
{
z3 = rnd.Next(1, 49);
}
z4 = rnd.Next(1, 49);
if ((z1 == z4) || (z2 == z4) || (z3 == z4))
{
z4 = rnd.Next(1, 49);
}
z5 = rnd.Next(1, 49);
if ((z1 == z5) || (z2 == z5) || (z3 == z5) || (z4 == z5))
{
z5 = rnd.Next(1, 49);
}
z6 = rnd.Next(1, 49);
if ((z1 == z6) || (z2 == z6) || (z3 == z6) || (z4 == z6) || (z5 == z6))
{
z6 = rnd.Next(1, 49);
}
}
The code is working for me currently, but I think there is a better, much shorter way what my code does. That's why I am asking for advice or for an idea, how to do it better than me.
Thanks in advance.
CodePudding user response:
One way to solve it would be to simulate what a real lotto machine does.
First, put the 49 balls in the bucket:
var bucket = Enumerable.Range(1, 49).ToList();
Then in a loop, determine a random index in the current bucket, get the number at this index and remove it so that it cannot be drawn again
var random = new Random();
for (var i = 0; i < 6; i )
{
var index = random.Next(bucket.Count);
var number = bucket[index];
Console.WriteLine(number);
bucket.RemoveAt(index);
}
CodePudding user response:
Simple code with recursive call:
Random rnd = new Random();
List<int> listWinningnumbers = new List<int>();
for(int cont = 1; cont <= 6; cont )
{
SetNumbersWinners(listWinningnumbers);
}
private void SetNumbersWinners(List<int> listWinningnumbers)
{
int luckynumber = rnd.Next(1, 49);
if (listWinningnumbers.Exists(w => w == luckynumber))
{
SetNumbersWinners(listWinningnumbers);
}
else
{
listWinningnumbers.Add(luckynumber);
}
}
CodePudding user response:
This example draws 6 unique random lotto numbers from a possible range of 1-49
class Program
{
public static readonly Random rng = new Random();
static void Main(string[] args)
{
Console.WriteLine("Today's Lotto Numbers Are:");
var lotto = DrawLotto(6);
Console.WriteLine(string.Join(",", lotto));
//42,23,4,46,8,43
}
public static int[] DrawLotto(int count, int maxNumber = 49)
{
// Generate lotto numbers sequentially.
int[] result = Enumerable.Range(1, maxNumber).ToArray();
// Generate random numbers between 0-1
double[] random = result.Select((i) => rng.NextDouble()).ToArray();
// Sort the lotto numbers using the random numbers for ordering
Array.Sort(random, result);
// Return the first 'count' numbers requested
return result.Take(count).ToArray();
}
}
I am using a 4 step process, as outlined in the comments of DrawLotto()
. The idea is to create a shuffled array of all possible numbers in the drawing and return only the first 6. The shuffling process guarantees uniqueness and randomness. The above can easily be extended to other variations, but modyfing the maxNumber
parameter, or the count
argument.