# Thread: Unity 3 Blueprints - Heiro Match, Here is how to randomise the tiles at the start.

1. ## Unity 3 Blueprints - Heiro Match, Here is how to randomise the tiles at the start.

Hey guys, I have never been any good at coding so I bought "Unity 3 Blueprints" book. There I was at the end of Heiro part 1 and as I have only used Javascript once before I thought I would stop here and challenge myself.

You see if you are following along with the book you realise that the game Heiro has the tiles in the same places each time you launch the game. So I thought it would be a good idea to randomise the tiles so each game is different. So I did a lot of research and realised that there was no help out there to randomize the array used. So I created my own.

Here is the code:-
Code:
```function randomizeArray()
{
for (count = 0; count < 100; count++)  // I chose to go through the loop 100 times, why not :D
{
Rand1 = Random.Range(0, tileObjects.length); // Get a random number between 0 and 16
Rand2 = Random.Range(0, tileObjects.length); // Get a random second number between 0 and 16
while (Rand1 == Rand2)
{
Rand2 = Random.Range(0, tileObjects.length);  // Stay in this loop until both numbers are different
}

temp = tileObjects[Rand1];  // Backup the first number
tileObjects[Rand1] = tileObjects[Rand2] ;  // replace the first number with the second one
tileObjects[Rand2] = temp ;  // replace the second number with the backup
}
}```
That's all there is to it really the only other thing you need is "randomizeArray()" command at the top of the Start() function. I know there is a second part to this game in the book, which I am about to do, but in case there was no randomise function I created my own.
Last edited by WindyBut_UK; 07-02-2011 at 06:13 PM.

2. Nice, my book arrived last week but not had time to go through it yet. Thanks for sharing your own code, it's very generous.

3. I know as a programming exercise it would take half a second to do but I have very little experience so it took me longer, a lot longer. Once you do go through the book you can find the assets needed www.deeppixel.com. Oh and keep an eye on the errata page for mistakes made in the book

4. Registered User
Join Date
Aug 2011
Posts
1
That's a really inefficient way to randomise (I just found this thread because I am using the book in a classroom and have set this as a challenge for my students (to randomise the array)).

My solution (which can be simplified even more)

Code:
```function RandomiseTileSet()
{
var arrayLength = tileObject.Length-1;
var arrayCurrentLength = arrayLength;
for (i = 0;i<arrayLength;i++)
{
var randomNum = Random.Range(0,arrayCurrentLength);
var temp = tileObject[randomNum];
tileObject[randomNum]=tileObject[arrayCurrentLength];
tileObject[arrayCurrentLength]=temp;
arrayCurrentLength--;
}
}```
It works, by picking a random element, putting it at the end of the array and swapping the last element with the random one. Then it reduces the length of the array it has to work on. It's a "random sort" where it sorts the back end of the array randomly. It could be done frontwards as well, but it seemed more intuitive to do it from back to front.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•