Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Picking 7 Random "Cards"

Guest
Feb 14, 2013 Feb 14, 2013

I am wondering if the following code would yield essentially the same results as dealing 7 cards from a sufficiently shuffled deck:

var deck:Array = new Array();

for(var j:int = 0;j<52;j++){

    deck.push(j);

}

var hand:Array=new Array();

var card:uint;

for(j = 0;j<7;j++){

    card = int(Math.random()*deck.length);

    hand.push(deck.splice(card,1));

}

If not it would be great to know:

1. Why?

2. How to get the desired results.

Cheers,

John

TOPICS
ActionScript
816
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Feb 14, 2013 Feb 14, 2013

Should work. To be consistent you should cast card in your for-Loop as uint.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 16, 2013 Feb 16, 2013

This definitely works but just using Math.random() doesn't make it really random.

A more reliable approach from randomization standpoint is using some shuffling algorithm. The most common is Fisher-Yates algorithm. Function shuffle in the code below implements this.

So, this code makes it more random. I organized tasks in different functions.

var deck:Array;

makeDeck();

shuffleDeck();

deal();

function makeDeck():void

{

          deck = [];

          for (var i:int = 0; i < 52; i++)

                    deck.push(i);

}

function shuffleDeck():void

{

          shuffle(deck);

          trace(deck);

}

function deal():void

{

          var hand:Array = deck.splice(0, 7);

          trace(hand);

}

function shuffle(array:Array):void

{

          var i:int = array.length;

          var j:int;

          var temp:*;

          while (i--)

          {

                    j = Math.random() * i;

                    temp = array;

                    array = array;

                    array = temp;

          }

}

Message was edited by: Andrei1 Changed deal() function - just splice is sufficient.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Feb 16, 2013 Feb 16, 2013

Thank you for taking the time on such a complete response. I am still left wondering how much more "random" this is in practice. I realize your method is accepted as a best practice. It just seems like a lot of extra processing. I've run some tests on my implementation and found it yields approximately the right ratio of four of a kinds to deals (around 1 in 600) so would I be wrong in accepting it as good enough?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 16, 2013 Feb 16, 2013
LATEST

I did not test randomness outcome aspects. There are many articles on Internet that describe in details why many programming languages random number generators are not really random and testing show huge deal of deviations.

The approach I showed has definitely much more random outcome. For one - there are 52 randomization attempts in the shuffle function in your case.

Fisher-Yates algorithm just makes a lot of common sense even from academic point of view. It is also very elegant and efficient. It is widely used no matter if one write code or does it on a piece of paper.

One shouldn't worry about efficiency here. Loops in AS3 are extremely fast and they can handle millions of iterations in a matter of single milliseconds. You are dealing with just an array with 52 elements only. It takes a fraction of millisecond to accomplish.

I am not sure why you perceive what is shown as a lot of processing. It is miniscule in terms of time it takes but it has clear specialization of functionality that has many advantages including a great deal of flexibility and scalability.

Definitely the code I showed can be abbreviated to meet your particular use cases.

But, at the end of the day, it is your call of course.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines