#how to get random item from array

1 messages · Page 1 of 1 (latest)

tired condor
#

im trying to put 1 player on a specific team then put every other player on another team

tired condor
#

how to get random item from array

sharp heron
#

Did you still need help with this?

errant granite
#

Get array name of the table then
do (tablename)[math.random(1,#game.Players:GetChildren()]

sharp heron
#

yup nice

#

although u can change #game.Players:GetChildren() to #tablename

fallow cloud
sharp heron
#

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())

fallow cloud
fallow cloud
sharp heron
#

my bad it may not be deprecated although it is no longer considered best practice

fallow cloud
sharp heron
#

okay let me check rq

fallow cloud
#

Random.new() is for seeded random bruh

sharp heron
#

math.random has seeded random too

fallow cloud
#

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

sharp heron
#
#

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

fallow cloud
sharp heron
#

ah I see

fallow cloud
#

and the response you linked makes a statement about "good and bad sequences" but doesn't explain what that is

sharp heron
#

thats interesting

fallow cloud
#

i think they mean sequences with low and high rolls in it but that's just what you should expect from rng.

sharp heron
#

I think they were reffering to the randomness

#

yeah

fallow cloud
#

no, it's all equal distribution randomness

sharp heron
#

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

fallow cloud
#

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

sharp heron
#

lol true

fallow cloud
sharp heron
#

it might just make like all servers in a game more likely to produce similar results over similar timespans of the games runtime\

sharp heron
fallow cloud
#

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?

sharp heron
#

I guess it depends if subsequent calls to :GetChildren() end up producing the list in the same order or randomized

fallow cloud
#

i overlooked the title facepalm

sharp heron
#

oh lol

fallow cloud
errant granite
tired condor
#

cuz i was reading it but i cant rly understand

errant granite
#

Whatever the variable for your table is

worldly spearBOT
#

studio** You are now Level 3! **studio

errant granite
#

Local tablename = {}

#

The name would be tablename

tired condor
#

Oh ok

tired condor
errant granite
#

Yep looks like it

tired condor
#

Ok thanks

#

I’m not home rn I’ll try it when I get back

tired condor
#

lemme ss

#

lemme print it to see what it thinks it is...

#

i printed it and it just came as nil

errant granite
#

Ig filter array doesn’t exist