Pick random number with exception

Get help using Construct 2

Post » Sun Jun 03, 2012 11:14 am

How to pick a random generated number, and the picked number will never be pickable later?

The idea is i want to create a game with many randomed questions, and i don't want the answered questions to appear again.

Is there a good way to do that?
B
19
S
5
Posts: 51
Reputation: 1,883

Post » Sun Jun 03, 2012 12:51 pm

An array would be a good way to store your questions, select them, and also mark whether a question has been used. Say you have 20 questions. Create an array with a width of 20 and a height of 2. Think of it as a grid with 20 columns (X dimension) and 2 rows (Y dimension). In the first row of each column, you put the text of a question. In the second row, you put a numeric marker, 0 for unused and 1 for used. Initially, all the markers would be set to 0. When a question is used, you have the program set its marker to 1. Then, if the random picker picks a question with a marker of 1, you tell it to pick again. It keeps trying until if finds a question with the marker still set to 0. Given the speed of modern computers, this will happen very fast, even if the program has to try again many times.
B
16
S
4
G
1
Posts: 40
Reputation: 2,705

Post » Sun Jun 03, 2012 12:55 pm

Read my tutorial on Generating random numbers without duplicates.   I made it just for you!

Basically, each time you generate a random number you will check to see if it is in an array and if it isn't you will insert it into the array and use it for your game. If it IS already in the array, try again.

Since you don't know the size of your array to begin with, you might want to push the value onto the end of the array. I'll try to post a variation that shows that today. When I do it will be #3 under the end notes.
kittiewan2012-06-03 12:57:36
B
29
S
11
G
12
Posts: 281
Reputation: 11,878

Post » Sun Jun 03, 2012 1:32 pm

Great tutorial @kittiewan, a lot of people will probably find this useful!
B
27
S
8
G
8
Posts: 903
Reputation: 8,452

Post » Sun Jun 03, 2012 3:26 pm

Cool! Thanks everyone! Thank @kittiewan for the tutorial, keep up the good work. :]
B
19
S
5
Posts: 51
Reputation: 1,883

Post » Sun Jun 03, 2012 4:37 pm

Thanks! I posted the link to a new project (RandomNosPush.capx) that shows how to generate a unique random number on demand. You'll find it along with a brief explanation at the very bottom of the Generating random numbers without duplicatestutorial. It is a more complete example. Hopefully you'll find the comments enough to explain what is going on.kittiewan2012-06-03 16:38:27
B
29
S
11
G
12
Posts: 281
Reputation: 11,878

Post » Sun Jun 03, 2012 4:52 pm

@kittiewan
This might be sligthly better, the overhead would just be on start of frame to fill the array if you have thousands of values.
samplingWithoutReplacement.capx
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 20 guests