This tutorial will answer two questions:
1. How to shuffle a list
2. How to generate a set of random numbers that don’t repeat, meaning unique numbers
We are going to make an app that will have 4 image sprites in a canvas. The sprites will be moving across the screen. In every 5 seconds, the speed of the sprites will change. But there will never be two sprites that will have the same speed. We want them to always have different speeds in any given time. Our app is going to look like this-
If you want to take a look at the app and play with it, download UniqueSpeedyBirds project file.
So how do you generate random numbers from a given range that are always unique?
1. Have a list of predefined numbers you want to pick from
2. Shuffle the list
3. Get the first item in the list or get all
In our app, we have 4 image sprites. We assigned 4 different images to those sprites. We want them to have any of these four predefined speeds – 10, 15, 25, 30. We have a Clock component that fires after every 5 seconds. When it fires, we change the speeds of the sprites making sure that each of them is assigned a different speed.
As you can see in the blocks image above, we have defined a list variable with our predefined speeds values. We also have a temporary variable that we’ll need later.
Before we explain how we shuffle a list, let’s take a look at the implementation first-
To shuffle, we will use Fisher–Yates shuffle algorithm. You should definitely read that algorithm. We loop through each index starting with 1 until the size of the list that was passed into ShuffleList procedure. Current index is represented by variable i in for each loop. For each i, we select a random index between 0 to length of list minus i. We add the random index that we got, to i. The new index is represented by the local variable replace. We then swap the values in list to get a new value at index i. We do that for each index in list.
Swap procedure takes three arguments. The list where to swap, the index which we will swap at, and the replace index that we will use to swap with. We used a temporary variable named tempValue in Swap procedure to store the current value before we swap. If we don’t, we will lose current value. We use the replace list item block of list to do the swapping.
This is what we do when the timer goes off-
Firstly, we don’t want to change our original SPEEDS list. That is why we used a temporary variable named tempData. We added each item from SPEEDS list to that temporary variable using a for each loop. Remember we cannot just do tempData = SPEEDS. In that case, if you change anything in tempData, the changes will be also made to SPEEDS. If you are wondering why, you should better start here. After we add each item to our temporary variable, we call ShuffleList procedure and pass the temporary variable so that the shuffle only takes place to tempData, not to SPEEDS list.
After shuffling, we assign the first bird’s speed to the value at index 1 of tempData. For second bird, we use the second index and so on.
Our birds move from right to left because in design view we set their Heading property to 180. Whenever they reach an edge, they bounce off meaning the heading changes. If they bounce from left, their heading will change to 0 heading right and if they bounce from right, their heading will change to 180 moving leftward.
Also we change the pictures depending on the heading. If they are moving rightward, we want the picture where the bird is facing right.
Download the project source – UniqueSpeedyBirds
This has been extremely useful.
Thank you for your efforts.
Thank you for all your tutorials. They are very helpul for me.
please explain the difference b/w
1:the initalise gloabal variable
2:intialise local name to in
3:the second type of intialise local name to in(there are two types of intialise local name to in)
Thanks a lot,
i use it for learning vocabulary, and in that : Tests with random questions out of a list.
I am glad that you found it helpful.
Hi Sajal, I need your advice on how to program a scroller in List Picker to stay on the same item in the list when users return to list picker from URL. This is the concept of my app, but the list in the listpicker is MUCH longer and the scroller always goes to the top of the list while I want it to stay at the last item when users return from a visited URL. Thanks a lot in advance!
hi, mr. sajal
i have one question for you how to display the image list AppInventor
hi, mr. sajal
how to display the many image in list with AppInventor
Check out the first tutorial – http://imagnity.com/tutorials/app-inventor/our-first-app-using-app-inventor/