Random Integer without duplicate integers?

Hi everyone,
I want to generate random integers for lottery grid, as you know, lottery grids can’t have same number twice, my workaround so far is to make as much random integers blocks as needed, and each has its own range, but it doesn’t look any good…

BLOCK:RandomInteger
  minimum = 1
  maximum = 3
  => VAR @r1
ENDBLOCK

BLOCK:RandomInteger
  minimum = 4
  maximum = 6
  => VAR @r2
ENDBLOCK

BLOCK:RandomInteger
  minimum = 7
  maximum = 9
  => VAR @r3
ENDBLOCK

BLOCK:RandomInteger
  minimum = 10
  maximum = 11
  => VAR @r4
ENDBLOCK

BLOCK:RandomInteger
  minimum = 12
  maximum = 14
  => VAR @r5
ENDBLOCK

BLOCK:RandomInteger
  minimum = 15
  maximum = 16
  => VAR @r6
ENDBLOCK

BLOCK:RandomInteger
  minimum = 17
  maximum = 18
  => VAR @r7
ENDBLOCK

BLOCK:RandomInteger
  minimum = 19
  maximum = 20
  => VAR @r8
ENDBLOCK

Is there anyway to make it without setting each one with its own range ?

You can write C# in the lolicode section and do something like this.

uniqueNumbers.Count < 8 - Set 8 to however many numbers you need to be generated

random.Next(1,100) - Set 100 to the highest number you want possible

image

After that, you can iterate through the list or make a foreach loop to get a unique number for each section.

1 Like

I got your DM but I’m posting the response here for public reference.

Here is the code snippet:

List<int> uniqueNumbers = new List<int>();
Random random = new Random();
while (uniqueNumbers.Count < 8)
{
    int randomNumber = random.Next(1, 100);
    if (!uniqueNumbers.Contains(randomNumber))
    {
        uniqueNumbers.Add(randomNumber);
    }
}

And you can access any part of the list like this:

This does not have to be done in a constant, this is just for an example.

Keep in mind that lists in C# start at 0 and go up. So a list with 8 entires goes 0-7.

You can put that value in any block you want. Hope this helped.

2 Likes

Awesome, thank you so much.
I suppose they’re stored as list and it works the same as arrays ? So basically I would have to call uniqueNumbers[0] , uniqueNumbers[1] and so on, as needed, until grid is full?

Yes, pretty much. Unless you want to do a for each loop that index’s through the entire list.

2 Likes