random number

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Le_juiceBOX
Citizen
Posts: 71
Joined: Sat Mar 26, 2016 3:07 pm

random number

Post by Le_juiceBOX »

how would i make a randomly generated number?
SteamLibrary-like Program For Love2D Games:
take me to the forum thread!
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: random number

Post by ivan »

Code: Select all

n = math.random() -- floating point in range 0 to 1

n = math.random(e) -- integer in range 0 to e

n = math.random(s, e) -- integer in range s to e
User avatar
pgimeno
Party member
Posts: 3551
Joined: Sun Oct 18, 2015 2:58 pm

Re: random number

Post by pgimeno »

Or even better, use love.math.random instead, which unlike Lua's, is pre-initialized before calling love.load.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: random number

Post by zorg »

pgimeno wrote:Or even better, use love.math.random instead, which unlike Lua's, is pre-initialized before calling love.load.
While that is true, it isn't the main reason why it's better; it's consistent across platforms, which matters way more.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Jasoco
Inner party member
Posts: 3725
Joined: Mon Jun 22, 2009 9:35 am
Location: Pennsylvania, USA
Contact:

Re: random number

Post by Jasoco »

Yes, there are numerous ways to get a random number:

Lua's default math.random() accepts two parameters to return a number from (Say a random number from 1 to 10 or -50 to 50.) or you can leave the parameters out and it will simply return the decimal value it uses to pick those numbers. That will be a decimal between 0 and 1. But the drawback is that math.random is not consistent across platforms. So if you're trying to make a game for multiple platforms that you might want to generate the same on each, it won't work. Because OS X, Windows, Linux, iOS, Android, etc will all return different values from the same seed.

Which is where Löve's own Math functions come in. There are multiple ways to get random values with Löve's features:

First is love.math.random which is exactly the same as math.random except that it is consistent. It will always generate the same values from the same seed on each platform. If that is important to you. Reasons this would be important, well look at Minecraft. It is important for world generation to be consistent in Minecraft. A world generated from a seed on a Windows machine needs to look the same on OS X and Linux.

Second is the ability to create multiple RNGs using love.math.newRandomGenerator(), or "Random Number Generators" so you can have different RNGs for different aspects of the game. Using the same Minecraft example above, you will want one RNG for the world. (This one is actually Noise generation, I'll get to that) One for picking when to place an entity or a tree. One for picking what entity or tree to make. Or how many ores are in a vein, etc... So having multiple RNGs is very useful in games like that where you want to let the player pick a seed to share with friends.

Lastly is love.math.noise. This creates a value from 0 to 1 based on 1, 2, 3 or 4 parameters. And outputs values in a Perlin Noise pattern. For instance, also using the Minecraft example above, the world might be generated from the cloudy looking noise returned by having maybe an X and a Y parameter. Noise doesn't accept a seed and always looks the same however. But it stretches on infinitely and can be used for other things like variations in terrain, i.e. picking whether a tile should look like the default design or use one of the varied tile designs to break up the monotony. A better example would be to look at Super Mario Maker. Notice how when you draw a large area of a single tile type, it might, depending on what tile or level type, have a different look. Like the Airship tileset where randomly the tile might have a porthole window in it to make it look more natural. You don't pick this porthole tile, it's chosen by the game using Noise. Minecraft's "Optifine" mod does this too, as does the vanilla game now, where it chooses a random rotation for grass tiles to break up the grid, or offsets certain flowers seemingly random. This is all noise. The grass tile will always be the same rotation if placed in that exact tile. The flower will always be offset at that position when placed on that tile. It gives life to a bland looking grid. Makes it more personal.

For math.random, Google "lua random number" and for Löve's documentation on love.math, check the Wiki in the Math category.
drunken_munki
Party member
Posts: 134
Joined: Tue Mar 29, 2011 11:05 pm

Re: random number

Post by drunken_munki »

ivan wrote:

Code: Select all

n = math.random() -- floating point in range 0 to 1

n = math.random(e) -- integer in range 0 to e

n = math.random(s, e) -- integer in range s to e
At first I saw this and thought something was wrong... then you made be question one and a half years of programming in Lua -- anyway I double checked it:

math.random(e) generates integer numbers between 1 to e

not 0 to e.

God bless.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: random number

Post by zorg »

Also, one more thing, löve's PRNGs work with Uniform probability distributions, and have a function for getting a value with Normal p. distribution as well; that said, there exist quite a few more, that you'd need to write algorithms for to compute, if you want a generator that outputs numbers behaving some other distribution instead.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: random number

Post by ivan »

Good catch, munki :)
First is love.math.random which is exactly the same as math.random except that it is consistent. It will always generate the same values from the same seed on each platform. If that is important to you.
Good point. I don't know much about RNGs but it might be cool to see a pure Lua RNG that is also consistent among Lua distributions (since I use a 32-bit build of Lua).
Much love to you all.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 59 guests