How to randomly choose from specific numbers

Greetings,

To briefly explain, I need to do something that is based on random numbers (it’s scratch tickets, but don’t worry, the currency cannot be bought with Robux just so if a moderator sees this). I would like to not use math.random since it’s impossible to organize. I have multiple numbers (which are the points sums that are written randomly in the scratched places): 1, 3, 5, 7, 10, 20, 35, 50, 75, and 100. Since scratch tickets in Roblox need to always give something back (and not leave the player empty-handed) I have to fill in every space. At the start, the game chooses one random number as the final money prize (for example 50 and it will occupy 3 random spaces (this mechanism is done by now and I used math.random). I need to occupy the rest of the 9 left spaces with the numbers listed above without placing the 50 again (the example number in this case. could be any one of the listed numbers above). I have to note that one of the numbers above has to appear 2 times or less (excluding the money prize number, in this case being 50, that appears 3 times since it’s the winner number), or else it would make no logic. Is there a function that I can use (without math.random) to get the number listed above randomly but without the final money prize number? Or if you have an idea about an algorithm I could use for this situation. It’s a pretty complex system. If you have any questions about the topic please ask me.

In your example, do you need other random number, but this number must not be the 50 number?

I would suggest first putting all the values in a table called numbers or whatever you want really, and randomly pick a winning number, using math.random then remove that value from the table, once it is removed using the same function to place the rest of the numbers removing the chosen number from the table that way it won’t be chosen again. something like this

local Numbers = {}
local firstNum

firstNum = Numbers[math.random(1, #Numbers)] 
2 Likes

Would something like this work? This picks a random number and then uses a second table to store the other numbers, which could be used to prevent the chosen number from appearing more times than you want it to.

local nums1 = {} -- put all the numbers here
local winner = nums1[math.random(1,#nums1)
local nums2 = {}
for val = 1, #nums1 do
if nums1[val] ~= winner then table.insert(nums2, nums1[val]) end
end

Edit: I just realised you asked for without math.random, although the math.random used in my example is likely the same as what you’re using now to select a prize number. ¯\_(ツ)_/¯

1 Like

Besides the winning number (which has to be shown 3 times, so that the thing actually makes sense), the other numbers have to be shown 2 times or less. For example, if the winning number is 50 and it is shown 3 times, then a normal number that is 10 cannot be shown in more than 2 spaces. That is one of my biggest problems right now. Meaning that a number can appear 2 or 1 times randomly, still in random spaces.

Ok a little confusing but I think I understand. What I would do then is make a sepperate function in deciding 3 random spaces and just paste the winning number in those places. Then use almost the same function to pick 2 random places that aren’t the first ones by using the same method as the function I showed you where you remove them from the table once they are selected

1 Like

Alright I’ll give you some info from my game to see what I am meaning in a visual way.

Those are the chances (I struggled making this with math.random)

I had to use math.random(1, 20) and then do (for example) if chance = 5 then value = 100 --(prize money, as an example). And that’s why I don’t want to use math.random.

The cells that you see that have the value 5 are the one with prize money. I have to replace the ones with 0 with the numbers that remain and I have to use a max limit of 2 numbers each to fill up. Again, using math.random may be even harder than doing the chance thing for the prize money.

And using math.random for every cell would make the user’s experience really slow, since they would use this feature a lot and it would consume a lot of memory to calculate everything in a short period of time, and may also give errors if stuff is done too slow or before an event.