Number random

Good morning

I’m reading in the various forums but I can not get the random working

I have 22 numbers, I have to get 5 random numbers different from each other, I would like to be able to print the numbers.

Can someone help me? thank you so much

You can’t get 5 random numbers that are guaranteed to be unique from a random number generator.

You can however, enforce this behavior with a shuffle bag.

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/shufflebag/

If you do this:

local numbers = {} for i = 1, 5 do numbers[i] = math.random(1,22)    print( i, numbers[i] ) end

numbers[1] … numbers[5] may contain the same values.  

You can guarantee no repetition using an SSK2 shuffle back as follows:

-- Create a bag, fill it, and shuffle it local bag = ssk.shuffleBag.new() for i = 1, 22 do bag:insert(i) end bag:shuffle()

Later, you can get values from the bag and it is guarnateed NOT to repeat for any sequence of 22 pulls from the bag. 

local numbers = {} for i = 1, 5 do numbers[i] = bag:get() print( i, numbers[i] ) end

Warning: It may repeat on the 23’rd request because it will automatically reshuffle and the last value picked may end up shuffling to the top of the bag.

To add to what @roaminggamer said, you want to create a list of numbers from 1 to 22 then shuffle them. Once shuffled you will get 5 numbers, they will be unique and in a random order.

Search the forums for numerous shuffle functions. @roaminggamer’s ssk library will to the trick nicely, but to educate you, you might want to find some other examples and study the shuffle code.

Rob

One simple way (AKA a simple hack) is to add each number generated in your loop to a string, next pass of the loop check your random value doesn’t exist in the string, if it does simply get a new random number.

As you are only talking a few numbers this hack is quick and easy.

Note: don’t do this for 1,000+ iterations

Thank you all for your help

I solved with the roaminggamer code.

Best regards

You can’t get 5 random numbers that are guaranteed to be unique from a random number generator.

You can however, enforce this behavior with a shuffle bag.

https://roaminggamer.github.io/RGDocs/pages/SSK2/libraries/shufflebag/

If you do this:

local numbers = {} for i = 1, 5 do numbers[i] = math.random(1,22)    print( i, numbers[i] ) end

numbers[1] … numbers[5] may contain the same values.  

You can guarantee no repetition using an SSK2 shuffle back as follows:

-- Create a bag, fill it, and shuffle it local bag = ssk.shuffleBag.new() for i = 1, 22 do bag:insert(i) end bag:shuffle()

Later, you can get values from the bag and it is guarnateed NOT to repeat for any sequence of 22 pulls from the bag. 

local numbers = {} for i = 1, 5 do numbers[i] = bag:get() print( i, numbers[i] ) end

Warning: It may repeat on the 23’rd request because it will automatically reshuffle and the last value picked may end up shuffling to the top of the bag.

To add to what @roaminggamer said, you want to create a list of numbers from 1 to 22 then shuffle them. Once shuffled you will get 5 numbers, they will be unique and in a random order.

Search the forums for numerous shuffle functions. @roaminggamer’s ssk library will to the trick nicely, but to educate you, you might want to find some other examples and study the shuffle code.

Rob

One simple way (AKA a simple hack) is to add each number generated in your loop to a string, next pass of the loop check your random value doesn’t exist in the string, if it does simply get a new random number.

As you are only talking a few numbers this hack is quick and easy.

Note: don’t do this for 1,000+ iterations

Thank you all for your help

I solved with the roaminggamer code.

Best regards