Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Should work. To be consistent you should cast card in your for-Loop as uint.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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?
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now