|
| 06 Aug 2017 03:20 PM |
this is supposed to print a random object in the table, but it keeps printing the last object in the table, any tips?
local rand=math.random(#array) print(array[rand])
print("print()") |
|
|
| Report Abuse |
|
|
Extuls
|
  |
| Joined: 02 Jan 2009 |
| Total Posts: 5557 |
|
|
| 06 Aug 2017 03:21 PM |
local rand = math.random(1, #array)
|
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 03:27 PM |
Above is stupid. You need to seed the pseudo-random generator with math.randomseed. The return value of tick is most commonly used:
math.randomseed(tick())
It may also be plausible to call math.random once, arbitrarily.
|
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 03:28 PM |
i dont think it matters, since math.random will automatically do a random value from how many the table has
print("print()") |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 03:37 PM |
seems to be printing second to last value @unsubtle
math.randomseed(tick()) local rand=math.random(1, #array) print(array[rand].."test11t1t1t1")
print("print()") |
|
|
| Report Abuse |
|
|
| |
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
| |
|
|
| 06 Aug 2017 03:50 PM |
wait then what did i do wrong
print("print()") |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 06 Aug 2017 04:15 PM |
just put this at the top of your script: math.randomseed(tick()) |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 04:24 PM |
i did, it seems to still choose a random value and it stays the same everytime its ran
print("print()") |
|
|
| Report Abuse |
|
|
Kodran
|
  |
| Joined: 15 Aug 2013 |
| Total Posts: 5330 |
|
|
| 06 Aug 2017 04:32 PM |
| try running uploading it and running it as an actual game and check the output with ### I vaguely recall some weirdness with randomness in studio vs in game. |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 04:38 PM |
@kod i did, its pretty clear im doing something wrong
array = {"hey", "hello", "sup"} math.randomseed(tick()) local rand=math.random(1, #array) print(array[rand].."test11t1t1t1")
print("print()") |
|
|
| Report Abuse |
|
|
badfitz67
|
  |
| Joined: 03 Jun 2010 |
| Total Posts: 13165 |
|
|
| 06 Aug 2017 04:45 PM |
array = {"hey", "hello", "sup"} math.randomseed(tick()) for i = 1, 50 do local rand=math.random(1, #array) print(array[rand].."test11t1t1t1") end |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 04:49 PM |
@badfitz wouldnt that just print a random num 50 times?
print("print()") |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 04:50 PM |
aaa = {1,2,3,4,5,6} print(aaa[math.random(1,#aaa)])
noobs get out
|
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 04:55 PM |
if i add randomseed, it chooses a new value and sticks with that one, it does the same without randomseed
print("print()") |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 04:57 PM |
array = {"hey", "hello", "sup"} local rand=array[math.random(1, #array)] print(rand.."test11t1t1t1") |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 04:58 PM |
is my studio ok
print("print()") |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 04:59 PM |
| idk i ran the script i provided a few times and it was all random |
|
|
| Report Abuse |
|
|
WXBZ
|
  |
| Joined: 10 Oct 2012 |
| Total Posts: 850 |
|
|
| 06 Aug 2017 05:00 PM |
local table = {"I", "Am", "Hostage", "Send", "Help"} math.randomseed(tick() * math.random()) print (tables[math.random(#tables)])
there ya go :)
for _, post in pairs(game.Players.WXBZ.Posts:GetChildren()) do post.Signature.Text = 'scripters.cf' end |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 05:07 PM |
this is gonna give me a heart attack, current status:
array = {"hey", "hello", "sup"} math.randomseed(tick()) local rand=math.random(#array) print(array[rand])
print("print()") |
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 05:13 PM |
I told you that you may need to run it arbitrarily...
|
|
|
| Report Abuse |
|
|
|
| 06 Aug 2017 05:17 PM |
can you give me an example? i've been playing around with it and im convinced somethings broken
print("print()") |
|
|
| Report Abuse |
|
|
| |
|
|
| 06 Aug 2017 05:57 PM |
| just use what i said.. you dont need math.randomseed |
|
|
| Report Abuse |
|
|