Re: [PHP] shuffle or mt_rand

2007-07-03 Thread Richard Lynch
On Fri, June 29, 2007 4:57 am, Ryan A wrote:
 Have a quick question about which is better to get real random values,
 shuffle or mt_rand.

I *thought* that shuffle used mt_rand under the hood, but could be
totally wrong...

As far as REAL random values goes, that's an encyclopedia of
information, really...

Unless there's real money on the table, shuffle or mt_rand should be
fine.

 I am trying to shuffle a deck of cards and reading another thread I
 saw that after a couple of thousand random generations patterns have
 been observed using rand()... so its better to use mt_rand().

 Any idea which would be better to constantly generate proper random
 numbers for a long period of time?

I'd have to go with Tijnema's code because it will be maintainable
long-term MUCH easier, and I doubt that either is significantly more
random than the other.

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[PHP] shuffle or mt_rand

2007-06-29 Thread Ryan A
Hey,

Have a quick question about which is better to get real random values, shuffle 
or mt_rand.

I am trying to shuffle a deck of cards and reading another thread I saw that 
after a couple of thousand random generations patterns have been observed using 
rand()... so its better to use mt_rand().

After i wrote a function (copied below) to get real random cards from a 52 
deck, Tijnema from the list kindly sent me a piece of code thats 
simpler/faster/should or would use less processing and does exactly what my 
function does except it uses shuffle()...

Any idea which would be better to constantly generate proper random numbers for 
a long period of time?


= My original code with mt_rand() 

function load_cards($no_of_players)
{
$i=0;
$cards=array();

while($i1)
{


if(empty($cards[0]))
{
$cards[]=mt_rand(1,52);
}
else
{
$no_of_cards_already_in_array=count($cards);
$cards_to_be_dealt=$no_of_players * 2; 
$cards_to_be_dealt=$cards_to_be_dealt + 5;

if($no_of_cards_already_in_array  $cards_to_be_dealt)
{
$new_generated_card=mt_rand(1,52);

if(!in_array($new_generated_card,$cards))
{
$cards[]=$new_generated_card;
}
}else
{
// end the loop
$i=2;
break; // just in case the above does not break outa the loop.
}

}
}
return $cards;
}


== Contributed code ===
// only generating 5 numbers here
$deck=array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52);
shuffle($deck);


for($i=0; $i5; $i++)
{
$card = array_pop($deck);
echo $card.br;
}

==


Any comments and suggestions are also welcome.

Thanks!
Ryan





--
- The faulty interface lies between the chair and the keyboard.
- Creativity is great, but plagiarism is faster!
- Smile, everyone loves a moron. :-)
   
-
Need a vacation? Get great deals to amazing places on Yahoo! Travel. 

Re: [PHP] shuffle or mt_rand

2007-06-29 Thread Richard Heyes

Ryan A wrote:

Hey,

Have a quick question about which is better to get real random values, shuffle 
or mt_rand.

I am trying to shuffle a deck of cards and reading another thread I saw that 
after a

 couple of thousand random generations patterns have been observed
 using rand()... so its better to use mt_rand().

Well they do different things - shuffle() randomises an array, whereas 
mt_rand() gives you a random number. If you want to randomise an array, 
use shuffle(). If you want a random number, use mt_rand().


--
Richard Heyes
+44 (0)844 801 1072
http://www.websupportsolutions.co.uk
Knowledge Base and HelpDesk software

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] shuffle or mt_rand

2007-06-29 Thread Tijnema

On 6/29/07, Ryan A [EMAIL PROTECTED] wrote:

Hey,

Have a quick question about which is better to get real random values, shuffle 
or mt_rand.

I am trying to shuffle a deck of cards and reading another thread I saw that 
after a couple of thousand random generations patterns have been observed using 
rand()... so its better to use mt_rand().

After i wrote a function (copied below) to get real random cards from a 52 
deck, Tijnema from the list kindly sent me a piece of code thats 
simpler/faster/should or would use less processing and does exactly what my 
function does except it uses shuffle()...

Any idea which would be better to constantly generate proper random numbers for 
a long period of time?



code



Any comments and suggestions are also welcome.

Thanks!
Ryan


Just did a quick benchmark for 10.000 hands (making a full deck on
your code, 23.5 players):
Microtime difference:
Ryan's code:15.725826978683
Tijnema's code:0.40006709098816

Unique decks out of 1:
Ryan's code:1
Tijnema's code:1

When making a full deck my code is 40 times faster, and a lot less
memory intensive. And as you can see, for both all 1 decks are
unique, so both are random :)

But, also when generating cards for only 4 players, my code is twice
as fast as yours, and both generate still 1 random decks:

Microtime difference:
Ryan's code:0.82403707504272
Tijnema's code:0.40426802635193

Unique decks out of 1:
Ryan's code:1
Tijnema's code:1


Tijnema

--
Vote for PHP Color Coding in Gmail! - http://gpcc.tijnema.info

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] shuffle or mt_rand

2007-06-29 Thread Fredrik Thunberg



snip


Just did a quick benchmark for 10.000 hands (making a full deck on
your code, 23.5 players):
Microtime difference:
Ryan's code:15.725826978683
Tijnema's code:0.40006709098816

Unique decks out of 1:
Ryan's code:1
Tijnema's code:1

When making a full deck my code is 40 times faster, and a lot less
memory intensive. And as you can see, for both all 1 decks are
unique, so both are random :)

But, also when generating cards for only 4 players, my code is twice
as fast as yours, and both generate still 1 random decks:

Microtime difference:
Ryan's code:0.82403707504272
Tijnema's code:0.40426802635193

Unique decks out of 1:
Ryan's code:1
Tijnema's code:1


Tijnema



Just a pointer:

Just because you get 1 different decks doesn't mean that it is random.

Since there are 52! (around 8*10^67) different decks and you choose 
1 of theese you would be very lucky to get 2 that are the same.

--
/Thunis

I refuse to answer that question on the grounds that I don't know the 
answer.

  --The Hitchikers Guide to the Galaxy

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] shuffle or mt_rand

2007-06-29 Thread Ryan A
Just did a quick benchmark for 10.000 hands (making a full deck on
your code, 23.5 players):
Microtime difference:
Ryan's code:15.725826978683
Tijnema's code:0.40006709098816

Unique decks out of 1:
Ryan's code:1
Tijnema's code:1

When making a full deck my code is 40 times faster, and a lot less
memory intensive. And as you can see, for both all 1 decks are
unique, so both are random :)

But, also when generating cards for only 4 players, my code is twice
as fast as yours, and both generate still 1 random decks:

Microtime difference:
Ryan's code:0.82403707504272
Tijnema's code:0.40426802635193

Unique decks out of 1:
Ryan's code:1
Tijnema's code:1



Hey Tijnema,
 
 I really had no doubt about your code being faster and cleaner... (as i 
mentioned in my original post) but can you send me the benchmarking scripts 
that you used so i may test them a bit more?
 
 Thanks!
 Ryan




--
- The faulty interface lies between the chair and the keyboard.
- Creativity is great, but plagiarism is faster!
- Smile, everyone loves a moron. :-)
 
-
Don't be flakey. Get Yahoo! Mail for Mobile and 
always stay connected to friends.

Re: [PHP] shuffle or mt_rand

2007-06-29 Thread Tijnema

On 6/29/07, Ryan A [EMAIL PROTECTED] wrote:

Just did a quick benchmark for 10.000 hands (making a full deck on
your code, 23.5 players):
Microtime difference:
Ryan's code:15.725826978683
Tijnema's code:0.40006709098816

Unique decks out of 1:
Ryan's code:1
Tijnema's code:1

When making a full deck my code is 40 times faster, and a lot less
memory intensive. And as you can see, for both all 1 decks are
unique, so both are random :)

But, also when generating cards for only 4 players, my code is twice
as fast as yours, and both generate still 1 random decks:

Microtime difference:
Ryan's code:0.82403707504272
Tijnema's code:0.40426802635193

Unique decks out of 1:
Ryan's code:1
Tijnema's code:1



Hey Tijnema,

I really had no doubt about your code being faster and cleaner... (as i
mentioned in my original post) but can you send me the benchmarking scripts
that you used so i may test them a bit more?

Thanks!
Ryan


Sure, you can look at the code here:
http://86.86.80.41/cards.phps

Tijnema





--
- The faulty interface lies between the chair and the keyboard.
- Creativity is great, but plagiarism is faster!
- Smile, everyone loves a moron. :-)


Don't be flakey. Get Yahoo! Mail for Mobile and
always stay connected to friends.





--
Vote for PHP Color Coding in Gmail! - http://gpcc.tijnema.info

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php