What are the difference between these functions?

Hi, can someone tell me the difference between the following:

Task.wait and wait()

math.random() and Random | Roblox Creator Documentation (E.g. :NextNumber, :NextInteger) et cetera

task.wait() is a slightly more optimised/accurate improvement compared to wait(), when wait() is called with no arguments the minimum wait duration is used which is 2 frames (around 0.03 seconds), when task.wait() is called with no arguments the minimum wait duration is used which is 1 frame (around 0.015 seconds), the difference is most noticeable when calling with no arguments (1 frame might not be a lot once but in a loop which runs potentially thousands of times the frames will stack quickly).

math.random() when called with no arguments returns a random number (double precision float value) between 0 and 1 inclusively (inclusively means 0 and 1 can be returned as well), you will most likely have a decimal value returned however. When math.random() is called with 1 argument, such as math.random(5) it will return a random integer between the range of 1 to the value specified by that argument, in this case 5. And finally when math.random() is called with 2 arguments, such as math.random(5, 10) it will return a random integer between the range specified by the values of the 2 arguments, in this case 5 and 10.

Random.new() is used to create a new random seed, you can pass an argument to it to set a specified seed, for example Random.new(0), each seed will generate pseudo-random numbers differently. Then once a seed has been created (randomly or set), you can call the methods :NextNumber()/NextInteger() through the Random class. Random:NextNumber() will use the current random seed to produce a random number (double precision float value or in other words a number with a decimal part), and Random:NextInteger() similarly will use the current random seed to produce a random integer (a whole number).

3 Likes

Please can you give an example of Random and NextInteger and NextNumber

1 Like

You can read about the task | Roblox Creator Documentation library in the initial announcement Task Library - Now Available!.

If you actually read his post you would see he described the behaviour of both functions.

I did read it, hence why I asked for an example, an illustration to demonstrate a way it can be used

It can be used in any situation where you might want a random variable for something.

Say you wanted to spawn the player at a random point in space, or choose a random element from a table, or calculating a random chance for a critical hit.

1 Like