Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I randomly select a certain number of different variables in a table?

So i've been toying around with string.len and I had the idea to make a string look 'Spooky' by having it randomly generate letters, numbers, and symbols from a table. Problem is I'm not quite sure on how to select random variables from the table with a certain number limit. (For example, if I used string.len("Bob") i'd want it to randomly pick 3 variables from the table.)

--[[
The list is very long & some of the symbols weren't showing up so I decided not to include it, but it's basically every letter, number, and symbol on the keyboard.
]]

while true do
	wait(0.01)
	local number = string.len(""..game.Players.LocalPlayer.Name)

	print(list[math.random(1, #list)]
end

Any help on how I can complete this task?

Edit:

I don't really know how to be specific, but here's my wanted and unwanted list: (Being the string.len("Example") turning into what I want & don't want.)

(My example list for these examples)


local list = {"1","2","3","4","5","6","7","8,"9","0"}

Wanted:
string.len("Bob") {Turns into} "903"
string.len("HiIamsusan!") {Turns into} "6172843509"

Unwanted:
string.len("Mike") {Turning into} "6" -then- "4" -then- "1" -then- "9"

Basically, I want it to be crammed together, the longer the string is in the string.len, the more items in the table it generates crammed together.

4 comments

1

Three different values? With or without replacement? Be specific! Give examples of what's acceptable and what's not

0

Edited.

0

To be clear, for input "Cat" is "121" a valid output? What about "111" and "112"?

0

print(("example string"):gsub(".", function(a) return math.random(3) == 1 and math.random(0, 9) or a end))`

2 answers

Assuming I'm understanding you correctly, you want every character replaced with a random number. Like 1waffle1 demonstrated in his comment, you could use gsub with it's optional function argument to extend the rules on how patterns are replaced. This will mostly be an extension on his comment. In his example, he replaces all characters randomly (each replacement having a 1/3 chance to actually take place). If you wanted to change this to only getting random elements from a list, it'd pretty much be the same except you'd return a random index from that list. Example:

local str = "billy"
local replaceWith = {"1","2","3"}

local function replaceWithElements(str, t)
	return str:gsub(".", function() -- '.' being the string pattern
		return t[math.random(#t)] -- return a random element to replace
	end)
end

print(replaceWithElements(str, replaceWith))

This would replace every character with a random element in a list, because the string pattern (.) just gets every single character in a string. This, however, may not be very favorable, as it will also replace space characters. I don't know if that's something you'd like to happen, or something you'd like to avoid, but here's how you could avoid that with a new string pattern:

local str = "billy"
local replaceWith = {"1","2","3"}

local function replaceWithElements(str, t)
	-- Instead of getting all characters (with .), we're not getting all characters between space characters. We're basically saying, get something that isn't a space, get all the characters in between, and make sure it stops at a non-space character.
	return str:gsub("%S.-%S-", function()
		return t[math.random(#t)]
	end)
end

print(replaceWithElements(str, replaceWith))

It may take a while to warm up to string patterns if you're new to them, but I hope I could clear things up a little, or at least provide some additional insight. If you have any questions, just let me know.

Just do a loop depending on the length of the string. Not much i can explain here, but if it makes any questions appear, feel free to post a comment.

local InputValue = "Hello, world!"
local result
local list = {"1","2","3","4","5","6","7","8,"9","0"}
for i=1,string.len(InputValue) do
     result = result + list[math.random(1,#list)]
end

-- do stuff with result

This discussion is preserved as it was. New answers, comments, and votes are closed.