playMaker

Author Topic: get unique Random int from int pool  (Read 5425 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15501
  • Official Playmaker Support
get unique Random int from int pool
« on: May 17, 2012, 10:24:28 AM »
Hi,

 Ok, this one might seem odd at first but it has infact a lot of use cases.

 So image a deck of card ( the current hot topic on this forum): you want to pick 5 of them to give the player a hand. You need a system that must not pick the same card again, this is an obvious statement but difficult to put into an algorithm if you are not used to scripting technics.

Based on that example, I made a simple action, that let you pick them cards.

let's say 10: we will have 1 2 3 4 5 6 8 9 10 to pick from, and we want to pick 5 of them

so this action will first pick let's say 3, the pool is now 1 2 4 5 6 7 8 9 10,
next pick, let's say 5, the pool is now 1 2 4 6 7 8 9 10
And so on

 so if we actually pick all of them ( pool count being equal to the serie count), we end up with a simple shuffle system.


 It works with ints, so you define first the range or pool to pick ints from. Why ints?, because they are easy to work with and most of the time represent a reference you work with.

 It's also something you can and should use with ArrayMaker, so that you can pick unique items from an array and be sure you don't get twice the same in the process, and you can use a ArrayListGet action and simply pass that int you got from this system.


If anyone has problems grasping this, let me know, and I'll do a working sample.

The action name is GetNextRandomInt
you can get this action on the Ecosystem, or preview its code source on Github



Bye,

 Jean
« Last Edit: August 15, 2015, 05:14:17 PM by jeanfabre »

Robbi

  • Playmaker Newbie
  • *
  • Posts: 2
Re: get unique Random int from int pool
« Reply #1 on: December 03, 2012, 08:38:52 AM »
Hi Jean,

I am indeed in need of this script, but I am a noob programmer.

Therefore, would need more of your thorough guide on how to install and use this script in a step by step instruction?

thanks a million, and sorry for the trouble...

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3962
  • Official Playmaker Support
    • LinkedIn
Re: get unique Random int from int pool
« Reply #2 on: December 03, 2012, 11:01:42 AM »
Hi Robbi,

I haven't used this action, but to install any action you just need to save it as a script in your project. E.g., create a file called GetNextRandomInt.cs anywhere in your assets folder and copy/paste the code into it.

This line in the code tells you where to find the action in the Action Browser:
[ActionCategory(ActionCategory.GameObject)]

You can then use this like any other action...

Robbi

  • Playmaker Newbie
  • *
  • Posts: 2
Re: get unique Random int from int pool
« Reply #3 on: December 03, 2012, 03:59:02 PM »
Hi Alex,

thanks for the guide.

 :D

then00b

  • Playmaker Newbie
  • *
  • Posts: 8
    • @then00b
Re: get unique Random int from int pool
« Reply #4 on: February 05, 2013, 11:41:36 AM »
This is beautiful. Thanks for this, Jean!
-Tony

djaydino

  • Administrator
  • Hero Member
  • *****
  • Posts: 6954
    • jinxtergames
Re: get unique Random int from int pool
« Reply #5 on: August 15, 2015, 07:44:15 AM »
Hi,
i have added this action to the ecosystem as it was not there yet.
And i added a reset option

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15501
  • Official Playmaker Support
Re: get unique Random int from int pool
« Reply #6 on: August 15, 2015, 05:12:28 PM »
Hi,

 Excellent, that's the spirit!!! Thanks :)

 Bye,

 Jean