Generating Random Numbers Without Duplicates

Generating unique random numbers is an essential technique for every game maker to know. This short tutorial demonstrates one way to use an array to generate a set of random numbers without duplicates.

The project requires Construct 2 Release 87 or later. The free version is fine.

You can download the complete project here:

http://dl.dropbox.com/u/57899112/RandomNos.capx

The goal is to insert 6 random integers that are greater than or equal to zero, but less than 15, into an array without any duplicates.

1. Create a new project and name it RandomNos.

2. Insert 3 objects into the layout: 2 text objects and an array. Change the objects' properties as shown below:

Name: txtTries

Plugin: Text

Position: 120,64

Size: 200,30

Name: txtValues

Plugin: Text

Position: 124,126

Size: 412,206

Text:

Name: Array

Plugin: Array

Width: 6

Once the objects are inserted your project should look something like this:

3. Switch to the event sheet and add an On start of layout event. Right click on the event and Add a blank sub-event. Your event sheet should look like this:

4. Click on the blank sub-event and right click and choose to Add a local variable as shown in the following figure. (If you don't see add local variable in the popup menu, click on the Events tab and try inserting it from the toolbar. Note that you can only insert a local variable in a place where it makes sense to use one.)

5. The variable will be added above the blank sub-event. Name it randomNumber and accept the defaults. Add two more local variables, i and tries. When you are done the event sheet should look like the following figure:

6. Modify the blank sub-event by double clicking on it and turning it into a While loop as shown below. Note that to create a while loop you first insert the while condition and then insert the condition that you want to be true.

7. Add some actions to the While loop as shown below. The first action generates a random integer greater than or equal to 0 and less than 15. (Because of the way the Random function works it will not actually generate the upper bound number. You can add 1 to the upper value if you want to include it.) The second action adds to the tries variable which stores the number of attempts made to insert unique random numbers into the array. The third action just displays how many attempts have been made.

8. Select the While event and right click to Add a sub-event with the condition Array Contains randomNumber. Right-click on the condition and Invert it so that actual test is to see that the array does NOT contain the random number.

9. Add some actions to the sub-event that will occur only if the random number is unique among the other numbers already in the array. First, insert the random number into the array at the position i. Then add a line to txtValues to show both i and the random number. Third, add 1 to i since we added a value to the array.

10. Finally, save and run the project. The result should look like the following figure. Notice that in this case it took eleven attempts to insert 6 random numbers into the array, and also notice that none of the numbers in the right column are duplicates.

That's all there is to it!

NOTES:

1) If you are generating a lot of random numbers there is the possiblity that your While loop could go on for a very, very long time. You may want to add a condition to set a limit to the number of tries as shown in the following figure. For this example it is unlikely you'd ever get close to 1000 tries, but it is a good thing to keep in mind.

2) If you are interested in a bare minimum version of the project, it would involve deleting the two text objects along with their associated events and actions, and it would look like the following figure.

Of course, without the text objects you won't see anything when you run this project.

3) The tutorial demonstrates how to generate all of the unique random numbers at once. If you want to generate them one at a time on demand you still use the same basic techniques, but in slightly different ways. To examine a project where a unique random number is generated on click or touch, check out the following project. In addition to realistically implementing a limit on the number of tries, it also shows how to push an element onto the end of an array.

RandomNosPush.capx

  • 0 Comments

  • Order by
Want to leave a comment? Login or Register an account!