generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: problem with printing random object in table

Previous Thread :: Next Thread 
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
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 is online. Extuls
Joined: 02 Jan 2009
Total Posts: 5557
06 Aug 2017 03:21 PM
local rand = math.random(1, #array)


Report Abuse
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
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
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
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
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
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
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
06 Aug 2017 03:47 PM
b


print("print()")
Report Abuse
Kodran is not online. Kodran
Joined: 15 Aug 2013
Total Posts: 5330
06 Aug 2017 03:48 PM
unsubtle is right
Report Abuse
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
06 Aug 2017 03:50 PM
wait then what did i do wrong


print("print()")
Report Abuse
Kodran is not online. 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
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
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 is not online. 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
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
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 is not online. 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
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
06 Aug 2017 04:49 PM
@badfitz wouldnt that just print a random num 50 times?


print("print()")
Report Abuse
LotsOfViolence is not online. LotsOfViolence
Joined: 29 Jun 2011
Total Posts: 8375
06 Aug 2017 04:50 PM
aaa = {1,2,3,4,5,6}
print(aaa[math.random(1,#aaa)])

noobs get out


Report Abuse
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
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
WatermelonMan4676 is not online. WatermelonMan4676
Joined: 20 Jan 2014
Total Posts: 5516
06 Aug 2017 04:57 PM
array = {"hey", "hello", "sup"}
local rand=array[math.random(1, #array)]
print(rand.."test11t1t1t1")
Report Abuse
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
06 Aug 2017 04:58 PM
is my studio ok


print("print()")
Report Abuse
WatermelonMan4676 is not online. WatermelonMan4676
Joined: 20 Jan 2014
Total Posts: 5516
06 Aug 2017 04:59 PM
idk i ran the script i provided a few times and it was all random
Report Abuse
WXBZ is not online. 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
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
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
Unsubtleties is not online. Unsubtleties
Joined: 28 Nov 2016
Total Posts: 9203
06 Aug 2017 05:13 PM
I told you that you may need to run it arbitrarily...


Report Abuse
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
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
yhatayhatahoohoo is not online. yhatayhatahoohoo
Joined: 19 Jan 2011
Total Posts: 1988
06 Aug 2017 05:41 PM
b


print("print()")
Report Abuse
WatermelonMan4676 is not online. WatermelonMan4676
Joined: 20 Jan 2014
Total Posts: 5516
06 Aug 2017 05:57 PM
just use what i said.. you dont need math.randomseed
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image