Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Getting a unique random number from within a table?

Asked by
Edenojack 171
8 years ago

Is there any method of creating a unique random number every time it is called from in a table? I'm currently using:

["Main"]=   CFrame.new(1.26865196, -0.568903923, -2.47046661, 0.995743334, 0.0894539207, -0.022211533, -0.0921702608, 0.96639806, -0.239957407, 5.04754247e-008, 0.240983203, 0.970529377),

as a means of animating a part, however trying to use something like:

["Main"]=   CFrame.new(math.random(1.24865196 , 1.28865196), -0.568903923, -2.47046661, 0.995743334, 0.0894539207, -0.022211533, -0.0921702608, 0.96639806, -0.239957407, 5.04754247e-008, 0.240983203, 0.970529377),

leaves me with only 1 random number, which is countinually called. Is there any way I can ensure that this is a random number?

0
Use a function or explain yourself better dude User#6546 35 — 8y

1 answer

Log in to vote
2
Answered by 8 years ago

Enter, functions

Whilst you're out of luck if you want to do this so that every time you access yourTable.Main it has the CFrame with a different random number, you can instead have Main be a function which returns the new generated CFrame.


math.random doesn't play nice with fractions

Yes, I know, annoying. You can probably use a lerp for this, however.

CFrame.new(1.24865196 + (1.28865196-1.24865196)*math.random(), -0.568903923, -2.47046661, 0.995743334, 0.0894539207, -0.022211533, -0.0921702608, 0.96639806, -0.239957407, 5.04754247e-008, 0.240983203, 0.970529377),

What that will do is choose a random number between 1.28865196 and 1.24865196 by using the fact that math.random already makes a fraction between 0 and 1, and applying it to a simple lerp.

0
Still can't get it to work. Appreciate the help though! Edenojack 171 — 8y
0
Did you use a function like I said? User#6546 35 — 8y
Ad

Answer this question