# Randomize tiles

**URL:** <https://community.aseprite.org/t/randomize-tiles/21614>\
**Category:** Scripts & Extensions\
**Created:** [March 3, 2024, 8:30pm UTC](https://community.aseprite.org/t/randomize-tiles/21614 "2024-03-03T20:30:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olga\_Galvanova](https://community.aseprite.org/user_avatar/community.aseprite.org/olga_galvanova/32/3294_2.png) [@Olga\_Galvanova](https://community.aseprite.org/u/Olga_Galvanova)\
**Post date:** [March 3, 2024, 8:30pm UTC](https://community.aseprite.org/t/randomize-tiles/21614/1 "2024-03-03T20:30:33Z")

</div>

**randomize non-empty tiles or change them all at once**  
_very simple script, but it does what it does._

![randomize-tiles-v0.01](https://community.aseprite.org/uploads/default/original/3X/0/4/04ee70e661c6716c5e53007b6af1eb4846e0b1fb.png)

before and after:  
 ![tileset-tilemap-colormode-test-1_00](https://community.aseprite.org/uploads/default/original/3X/a/b/ab5ff957ab7f281f47d5360bb3f2d47c75353cd6.png) ![tileset-tilemap-colormode-test-1_01](https://community.aseprite.org/uploads/default/original/3X/1/4/14509e0286412f4e280001a21008f56b8342ba38.png)

**HOW TO USE:**

1. create a tileset with some tiles in it
2. draw tiles on the tilemap layer
3. run script
4. set min and max values
5. click on ‘randomize’ button

**NOTEs:**  
– preview window needs refresh to update the changes (and app only shows them because of `app.refresh`)  
– min value must be smaller than max (lua…)  
– but you can set both to same value and use the script as flood-fill  
– you better make sure you don’t try to randomize empty cel or use non existent tiles. that’s on you, pal.

as usual, always make a backup before running anything.

```auto
-- RANDOMIZE TILES 0.01 
-- b236 
-- 
-- make a tile set and draw where you want the tiles to be 
-- run script 
-- set range of tile indices to randomize 
-- click on randomize 
-- 

local cel 
local rng_min = 1
local rng_max = 1

local img_h 
local img_w 

local dlg = Dialog{ title = "RANDOMIZE TILES 0.01 " }

dlg:number{
	id = "in_min",
	--label = "min",
	text = "min",
	decimals = 0,
	onchange = function() 
					rng_min = dlg.data.in_min
				end 
}

dlg:number{
	id = "in_max",
	--label = "max",
	text = "max",
	decimals = 0,
	onchange = function() 
					rng_max = dlg.data.in_max 
				end 
}

dlg:button{ 	
	id = "confirm_madness", 
	--label = string, 
	text = " RANDOMIZE ",
	selected = true,
	focus = true,
	onclick = function() randomize_tiles() end 
}

dlg:button{ 	
	id = " close ", 
	--label = string, 
	text = "close",
	selected = false,
	focus = false,
	onclick = function() dlg:close() end 
}

dlg:show{ wait=false }

function randomize_tiles() 
	cel = app.cel 
	img_h = cel.image.height
	img_w = cel.image.width 
	
	if cel.image.colorMode == ColorMode.TILEMAP then
		local tilemap = cel.image 

		for yy = 0, img_h-1, 1 do 
			for xx = 0, img_w-1, 1 do 
				local pxl = cel.image:getPixel(xx,yy) 
				local val = math.random(rng_min, rng_max)
				if pxl > 0 then 
					cel.image:putPixel(xx,yy, val) 
				end 
			end 
		end 
		app.refresh() -- uuu, naughty! 
	else 
		print("select tilemap!") 
		--dlg:close()
	end 
	
end 

```

---

<div class="post-metadata">

**Author:** ![SaQurata](https://community.aseprite.org/user_avatar/community.aseprite.org/saqurata/32/18690_2.png) [@SaQurata](https://community.aseprite.org/u/SaQurata)\
**Post date:** [June 2, 2024, 1:31pm UTC](https://community.aseprite.org/t/randomize-tiles/21614/2 "2024-06-02T13:31:26Z")

</div>

hey, Im not sure what you mean by “create tileset with some tiles in it” ive tried to run this script and it just keeps sending back “select a tilemap” thank you

---

<div class="post-metadata">

**Author:** ![Olga\_Galvanova](https://community.aseprite.org/user_avatar/community.aseprite.org/olga_galvanova/32/3294_2.png) [@Olga\_Galvanova](https://community.aseprite.org/u/Olga_Galvanova)\
**Post date:** [June 2, 2024, 1:35pm UTC](https://community.aseprite.org/t/randomize-tiles/21614/3 "2024-06-02T13:35:45Z")

</div>

well, you can’t randomize tiles when you don’t have any. and you can have tiles only in tilemap layer. so you have to create it first - either from scratch or by converting existing layer.

---

<div class="post-metadata">

**Author:** ![SaQurata](https://community.aseprite.org/user_avatar/community.aseprite.org/saqurata/32/18690_2.png) [@SaQurata](https://community.aseprite.org/u/SaQurata)\
**Post date:** [June 2, 2024, 2:21pm UTC](https://community.aseprite.org/t/randomize-tiles/21614/4 "2024-06-02T14:21:02Z")

</div>

I changed one of my layer into “tilemap” and it still doesnt work.

---

<div class="post-metadata">

**Author:** ![Olga\_Galvanova](https://community.aseprite.org/user_avatar/community.aseprite.org/olga_galvanova/32/3294_2.png) [@Olga\_Galvanova](https://community.aseprite.org/u/Olga_Galvanova)\
**Post date:** [June 2, 2024, 4:17pm UTC](https://community.aseprite.org/t/randomize-tiles/21614/5 "2024-06-02T16:17:52Z")

</div>

that’s weird, the fact you did get the message to select the tilemap implies the script is working. do you have the layer selected?

---

<div class="post-metadata">

**Author:** ![SaQurata](https://community.aseprite.org/user_avatar/community.aseprite.org/saqurata/32/18690_2.png) [@SaQurata](https://community.aseprite.org/u/SaQurata)\
**Post date:** [June 5, 2024, 3:46am UTC](https://community.aseprite.org/t/randomize-tiles/21614/6 "2024-06-05T03:46:46Z")

</div>

Im sure I have the layer selected. if what you mean is being on the “tilemap” layer

---

<div class="post-metadata">

**Author:** ![Olga\_Galvanova](https://community.aseprite.org/user_avatar/community.aseprite.org/olga_galvanova/32/3294_2.png) [@Olga\_Galvanova](https://community.aseprite.org/u/Olga_Galvanova)\
**Post date:** [June 5, 2024, 6:05am UTC](https://community.aseprite.org/t/randomize-tiles/21614/7 "2024-06-05T06:05:46Z")

</div>

yes. well… i’m honestly not sure, what’s wrong then.

---

<div class="post-metadata">

**Author:** ![dllnllb](https://community.aseprite.org/user_avatar/community.aseprite.org/dllnllb/32/18704_2.png) [@dllnllb](https://community.aseprite.org/u/dllnllb)\
**Post date:** [June 6, 2024, 9:05pm UTC](https://community.aseprite.org/t/randomize-tiles/21614/8 "2024-06-06T21:05:17Z")

</div>

Oh wow, I’m trying to make animations that emulate the look of various levels of EM radiation hitting a camera sensor, and placing tiles by hand was taking forever. This is just what I needed, thank you for making it!

---

<div class="post-metadata">

**Author:** ![Mr\_95](https://community.aseprite.org/user_avatar/community.aseprite.org/mr_95/32/20339_2.png) [@Mr\_95](https://community.aseprite.org/u/Mr_95)\
**Post date:** [January 14, 2025, 3:18am UTC](https://community.aseprite.org/t/randomize-tiles/21614/9 "2025-01-14T03:18:18Z")

</div>

Hey, stupid question but when I try to use this I get an error

Random Tile Fill.lua:64: attempt to index a nil value (upvalue ‘cel’)

Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![Olga\_Galvanova](https://community.aseprite.org/user_avatar/community.aseprite.org/olga_galvanova/32/3294_2.png) [@Olga\_Galvanova](https://community.aseprite.org/u/Olga_Galvanova)\
**Post date:** [January 14, 2025, 9:02am UTC](https://community.aseprite.org/t/randomize-tiles/21614/10 "2025-01-14T09:02:04Z")

</div>

hi there, it seems like you don’t have a tile layer selected.

 ![image](https://community.aseprite.org/uploads/default/original/3X/e/4/e476f962e84ea81d91da82cc9484e15e5e755acc.png)

---

<div class="post-metadata">

**Author:** ![Mr\_95](https://community.aseprite.org/user_avatar/community.aseprite.org/mr_95/32/20339_2.png) [@Mr\_95](https://community.aseprite.org/u/Mr_95)\
**Post date:** [January 14, 2025, 12:21pm UTC](https://community.aseprite.org/t/randomize-tiles/21614/11 "2025-01-14T12:21:01Z")

</div>

Oh thanks, I tried it several times and then updated asprite, it works fine in newer versions

---

<div class="post-metadata">

**Author:** ![Olga\_Galvanova](https://community.aseprite.org/user_avatar/community.aseprite.org/olga_galvanova/32/3294_2.png) [@Olga\_Galvanova](https://community.aseprite.org/u/Olga_Galvanova)\
**Post date:** [January 14, 2025, 12:34pm UTC](https://community.aseprite.org/t/randomize-tiles/21614/12 "2025-01-14T12:34:04Z")

</div>

good to hear that, i just realized i’m stil at 1.3.9
