Pick random number with exception

0 favourites
  • 7 posts
From the Asset Store
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
  • 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?

  • 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.

  • Read my tutorial on Generating random numbers without duplicates.   I made it just for you! <img src="smileys/smiley2.gif" border="0" align="middle">

    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.

  • Great tutorial kittiewan, a lot of people will probably find this useful!

  • Cool! Thanks everyone! Thank kittiewan for the tutorial, keep up the good work. :]

  • 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.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)