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

Is it okay if I ask for a full tutorial on math.random?

Asked by 7 years ago

Okay first of all please don't give me this link "http://wiki.roblox.com/index.php?title=Random_numbers" I read it all ready. Thats why I'm here.

So basically I just want a full tutorial because I have trouble with playing random sounds on an npc. I don't want to know how I would do it exactly I want to learn math.random and do it myself,So I can learn more.Hopefully This Isn't a request ;-;

0
why -1?? extremeyes 10 — 7y
0
What exactly is your problem with playing the sounds? M39a9am3R 3210 — 7y

2 answers

Log in to vote
3
Answered by 7 years ago
Edited 7 years ago

There's not a lot to math.random, so I may as well tell you as much as you probably need to know.

Basics

There are 3 ways to use math.random. The most useful way is this:

local Number = math.random(min, max)

where min is the minimum and max is the maximum. For example, in this code:

local Number = math.random(3, 11)

Number will be a number between 3 and 11, inclusive.

There are some shorter ways to use it, for example:

local Number = math.random(max)

will return a number between 1 and max, and this:

local Number = math.random()

will return a number between 0 and 1.

However...

As the wiki article states, the number generated depends on what the last one generated was. This means that the sequence of numbers will be the same every time you run the game. To get around this, you use the math.randomseed function. Usually, you want to set it to some external influence, like the value of tick():

math.randomseed(tick())

Hopefully this explains everything you need to know.

0
Note that setting the seed more than once is ill-advised, it will simply give you the same or similar results over and over. RemasteredBox 85 — 7y
0
Oh so it will randomly choose a number between min and max ? And thanks :D extremeyes 10 — 7y
Ad
Log in to vote
1
Answered by
joalars2 107
7 years ago

This isn't really a good tutorial, but oh well.

If you want random sounds on a npc, you may want to use a table with the sound ID's in it, and then select a random one from it, and insert it into a sound.

Incase you dont know what a table is, this is one.

local table = { var1, var2, var3 }

As for math.random() you'll want to use something that automatically detects how many things are in a table, just because you dont need to change the math.random() after adding more sounds that way.

local var4 = math.random( 1, 10 )

This will pick a random number from 1 to 10 that var4 will become. To use the automatical table content amount detection as stated before, it's really simple!

Just switch out the 10 in math.random( 1, 10 ) with #table. Doing so will make the math.random() go from 1 to however many things there are in the table.

local soundtable = { sound1, sound2, sound3, sound4 }

local ran = math.random(1,#soundtable)

sound.SoundId = soundtable[ran]

In the code stated above, soundtable[ran] checks the table "soundtable" for any content at whatever number "ran" is.

Alternatively, to shrink the code, you can use the following

local soundtable = { sound1, sound2, sound3, sound4 }

sound.SoundId = soundtable[math.random( 1, #soundtable )]

Hopefully this helped in some way :|

Also, you can use math.random in a variety of different ways, the way i just showed is just the one I prefer.

Another more inefficient but less advances way, is the following

local var = math.random(1,3)

if var == 1 then
    sound.SoundId = "id1"
elseif var == 2 then
    sound.SoundId = "id2"
elseif var == 3 then
    sound.SoundId = "id3"
end

im bad at doing tutorials allright?

Answer this question