2

# How do I use Random.new(), Tick(), and what are 'seeds'?

zomspi 546
1 year ago

What the title says.

8
Ziffixture 6785
1 year ago
Edited 9 months ago

These correlate to pseudo-random-number-generation or PRNG... A computer is too precise to be capable of pure random calculations, though, it can mimic such a thing through an algorithm comprised of mathematical formulas that produce sequences of subsequently "random" numbers.

There is just one issue with this: as all formulas, they are static. This means the PRNG result will always be the same, so how do we produce a different, reliable result every time? We use a seed. This is a dynamic, bias-number, that is used as a base for how the algorithm creates our result; a number that is never the same, integrated into certain placeholders within each formula.

tick(), can provide us this dynamic number. The function will return to us the current time elapsed from the Unix Epoch—a counter incremented every second, since January 1st, 1970 UTC—and is a common method used to find the time passed from a given point; because of this large dynamic number, it is also commonly used in PRNG.

This function is practically equivalent to os.time(). In many other languages—though the functions vary—this practice is still used for creating reliable seeds.

Random.new():

Random.new() creates a PRNG Object. It bares two methods known as :NextInteger(min, max) and :NextNumber(min=0, max=1), 0-1 as default (see differences through provided hyperlink). This Object can be locally initialized with a seed, as opposed to math.random's strictly internally-required entropy, that has to be modified with the .randomseed(seed) function which ultimately affects the global seed. It is preferred that you use Random over math.random, as it has numerous superior internal differences, stronger applicational abilities, and is ultimately more amicable.

Let's take a look at how it all comes together:

local RandomNumber = Random.new(tick()):NextInteger(1,5)
print(RandomNumber) --> 3


I hope this helps you get a better understanding! If you have any further questions, please comment below! Don't forget to accept this answer and upvote if you wish!

2
Amazing reliable source answer! maxpax2009 335 — 1y