The Daily Click ::. Forums ::. Klik Coding Help ::. How do I prevent duplicate random numbers
 

Post Reply  Post Oekaki 
 

Posted By Message

Road Kill 1987



Registered
  29/07/2009
Points
  134
28th October, 2011 at 22:44:23 -

Hi guys, I know this is a bit of a newbie question but how do I prevent duplicate random numbers, I'm trying to make a little game for young kids in which you have to pick the right box out of a number of other boxes. I'm using counters to store these numbers.

would anyone be able to help me with this as I'm getting a little confused

 
Xbox live Gamertag: Road Kill 1987

Windybeard Games



Registered
  14/04/2005
Points
  219

You've Been Circy'd!VIP MemberCandy Cane
28th October, 2011 at 23:53:53 -

An easy way to do this would be to do something like......

(say you have 5 numbers)

Start of frame --- Set counter 1 to random(5), Set counter 2 to random(5), set counter 3 to random(5) and so on...

Then do...

counter 2 = counter 1 ---- set counter 2 to random(5)
counter 2 = counter 3 ---- set counter 2 to random(5)
counter 2 = counter 4 ---- set counter 2 to random(5)
counter 2 = counter 5 ---- set counter 2 to random(5)

and so on for all other counters.

This is by no means the best way to do it but it is very basic and would give you what you need in as much as all random with no duplication. A much more efficient way would be to use fast looping.

 
n/a

Sketchy

Cornwall UK

Registered
  06/11/2004
Points
  1970

VIP MemberWeekly Picture Me This Round 43 Winner!Weekly Picture Me This Round 47 WinnerPicture Me This Round 49 Winner!
29th October, 2011 at 16:11:07 -

+ Start of frame
-> Start fastloop "randomize" 5 times

+ On loop "randomize"
+ Counter: flag 0 is OFF
+ Counter: pick one at random
-> Counter: set value to loopindex("randomize")
-> Counter: set flag 0 to ON

Note: The order of conditions is important - you must check the flag state before picking a counter at random.
If the counters are not duplicates of a single "counter" object, then you'll need to give them all the same qualifier.

 
n/a

Fifth

Quadruped

Registered
  07/05/2003
Points
  5815

VIP MemberGOTW JULY 2010 WINNER!Kliktober Special Award TagGOTW HALLOWEEN 2011 WINNERPicture Me This Round 51 Winner!
29th October, 2011 at 19:44:23 -

Sounds like you want a random number pool.

There are a couple of objects that do exactly that, if you're willing to use 'em. The Random Pool Object, and the Random Multipool Object (which I think is a newer version of the Random Pool). I've never really had much reason to use 'em, but they shouldn't be too hard to get to work.

 
Go Moon!

UrbanMonk

BRING BACK MITCH

Registered
  07/07/2008
Points
  49566

Has Donated, Thank You!Little Pirate!ARGH SignKliktober Special Award TagPicture Me This Round 33 Winner!The Outlaw!VIP MemberHasslevania 2!I am an April FoolKitty
Picture Me This Round 32 Winner!Picture Me This Round 42 Winner!Picture Me This Round 44 Winner!Picture Me This Round 53 Winner!
30th October, 2011 at 03:10:33 -

What is the range of the numbers?

If you need a range of numbers in a random order than just add that range to a list object then select random numbers from that list and add them to a second list.

Done.

 
n/a

Assault Andy

Administrator
I make other people create vaporware

Registered
  29/07/2002
Points
  5686

Game of the Week WinnerVIP Member360 OwnerGOTM JUNE - 2009 - WINNER!GOTM FEB - 2010 - WINNER!	I donated an open source project
30th October, 2011 at 04:35:33 -


Originally Posted by . : UrbanMonk : .
What is the range of the numbers?

If you need a range of numbers in a random order than just add that range to a list object then select random numbers from that list and add them to a second list.

Done.



This is very simple and effective way of doing it which I have used many times in the past.

 
Creator of Faerie Solitaire:
http://www.create-games.com/download.asp?id=7792
Also creator of ZDay20 and Dungeon Dash.
http://www.Jigxor.com
http://twitter.com/JigxorAndy

Road Kill 1987



Registered
  29/07/2009
Points
  134
30th October, 2011 at 11:11:49 -

Thanks for all of the replys, never expected so many.

I did a google search about it before posting here and urbanmonk's idea came up but for VisualBasic but couldn't make head or tails about it.

 
Xbox live Gamertag: Road Kill 1987

Road Kill 1987



Registered
  29/07/2009
Points
  134
30th October, 2011 at 14:07:58 -

Just to let you guys know that Sketchy's code works best for me, thanks to all of you for replying.

 
Xbox live Gamertag: Road Kill 1987
   

Post Reply



 



Advertisement

Worth A Click