#how to get random item from array
1 messages · Page 1 of 1 (latest)
how to get random item from array
Did you still need help with this?
Get array name of the table then
do (tablename)[math.random(1,#game.Players:GetChildren()]
what has rng got to do with this..
and u can use Random.new() instead of math.random as it is deprecated
for optimal randomness, you can use the current timestamp as a randomseed generator
local r = Random.new(tick())
filter the array by value like for pairs(player:getplayers)do if player==target_player then dotargetplayerstuff() else doeveryoneelse() end and you can decide the 1 player however you want.
math.random isn't deprecated..?
my bad it may not be deprecated although it is no longer considered best practice
this is facts
it has always been considered best practice jeez where are you getting your information from
okay let me check rq
Random.new() is for seeded random bruh
math.random has seeded random too
technically it's a singleton sure, but that doesn't really change the point of it
random.new is specifically for concurrent seeded random generators independent of global rng, which is what the math.random/randomseed is for
like terrain generation for example, re-generating chunks would use a random.new instead of math.random
or random:copy
Both are fine. But you probably shouldn’t be doing Random.new():NextNumber(0,10) like you suggested in your title. Each successive call of NextInteger (or similar) to a Random object is supposed to generate “good” pseudorandom numbers. There’s no guarantee that a sequence of the first calls to newly created Random objects has the same pr...
Yeah I guess I never considered the actual pros vs cons but I just started using Random.new() when I saw it one day, it seemed cleaner I guess
I thought I saw somewhere it was best practice but I think it's just something I started doing a while ago and never stopped
"The one big advantage of Random is that you can have different RNGs with different states, which is useful if you’re trying to generate the same sequence of numbers from the same seed." - ThanksRoBama
the very next response says:
I guess you could look at it this way too: math.random() is global and Random.new() is local.
sounds familiar
ah I see
and the response you linked makes a statement about "good and bad sequences" but doesn't explain what that is
thats interesting
i think they mean sequences with low and high rolls in it but that's just what you should expect from rng.
no, it's all equal distribution randomness
no not the algorithim
the code logic, like if someone uses randomseed or not
but I think they didn't know that they both use the same algorithim when they speculated on the effective randomness of Random
it would have very niche and specific consequences if you continually set randomseed to 1 every frame but most people wouldn't immediately be able to tell
lol true
yeah they're the same, calling them global and local is a good way to put it
it might just make like all servers in a game more likely to produce similar results over similar timespans of the games runtime\
learned something new thanks!
anyway the original question was about filtering in a for loop, rng wasn't related to anything here 🤷
glad you learned something along the way, is that not what it was always about?
what would be a better way of ensuring randomness when setting one of the players as the 'killer'
I guess it depends if subsequent calls to :GetChildren() end up producing the list in the same order or randomized
i overlooked the title 
oh lol
this is correct then. the filtering is also a problem, something to look out for. 
The post has random in the title
hey ik this is late but how do u get the tablename
cuz i was reading it but i cant rly understand
Whatever the variable for your table is
** You are now Level 3! **
Oh ok
So the table name would be the
returnarray = {}
Yep looks like it
ok im home now and i tried it but i got this error
lemme ss
lemme print it to see what it thinks it is...
i printed it and it just came as nil
Ig filter array doesn’t exist
