[Solved] Math.random creates a weird pattern

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
ZodaInk
Prole
Posts: 18
Joined: Thu Jan 08, 2015 5:18 am

[Solved] Math.random creates a weird pattern

Post by ZodaInk »

I have no idea how this happens or even if this is a problem with Löve. While the pattern is almost random, it is not the kind of random I want.
Tell me if it works fine for you or if you find something in the code that is wrong.
The .love file creates a grid and every tile in that grid has a 90%(ish) chance of being a box, instead it creates this pattern...
The orange boxes are love.math.random(), the blue boxes are the regular math.random().

This is what it looks like when I run it:
Image
Image
game.love
(777 Bytes) Downloaded 127 times
I am genuinely confused...
Last edited by ZodaInk on Tue Jan 26, 2016 8:34 pm, edited 1 time in total.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Math.random creates a weird pattern

Post by airstruck »

Why are you storing the random locations in 2d arrays? Wouldn't it be much simpler to store coordinate pairs in a 1d array?

Code: Select all

	for x = 0, math.floor(window.w / size) do
		for y = 0, math.floor(window.h / size) do
			if math.random() < 0.9 then
				index[#index + 1] = { x = x, y = y }
			end
		end
	end

Code: Select all

	for _, pos in ipairs(index) do
		love.graphics.rectangle("line",
		    pos.x * size + 2, pos.y * size + 2, size - 4, size - 4)
	end
User avatar
ZodaInk
Prole
Posts: 18
Joined: Thu Jan 08, 2015 5:18 am

Re: Math.random creates a weird pattern

Post by ZodaInk »

I usually would, don't know why I did it like this, but that is beside the point.
The point is that it doesn't create a noise-ish array of boxes, it's not even filling up anywhere near 90% of it.

Code: Select all

if math.random() < 0.9 then --[[code]] end
That should be true roughly 90% of the time, yet in my case it isn't.
User avatar
airstruck
Party member
Posts: 650
Joined: Thu Jun 04, 2015 7:11 pm
Location: Not being time thief.

Re: Math.random creates a weird pattern

Post by airstruck »

It's not beside the point, though. If you write code that's simple and easy to understand, it's probably going to work and you don't have to spend time trying to figure out why it doesn't work.
ss.png
ss.png (13.51 KiB) Viewed 2993 times
User avatar
ZodaInk
Prole
Posts: 18
Joined: Thu Jan 08, 2015 5:18 am

Re: Math.random creates a weird pattern

Post by ZodaInk »

That did fix it...
Now I am even more confused, why would this fix it?
Xugro
Party member
Posts: 110
Joined: Wed Sep 29, 2010 8:14 pm

Re: Math.random creates a weird pattern

Post by Xugro »

Your idea of saving a sparse matrix is flawed. I just saved the full matrix and it works (see attached code). If you fill in 90% of the boxes then it is better to store the complete matrix.

Edit:
Rici Lake wrote:ipairs iterates over integer keys, starting from 1, until it finds one which is not in the table.
Source: http://lua-users.org/lists/lua-l/2004-12/msg00311.html

And since you have a table where not all rows are present your drawing function will stop at that row and does not print the boxes below.
Attachments
game_repaired.love
(841 Bytes) Downloaded 106 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 75 guests