less code needed

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

less code needed

Post by Vegan »

after discussing issues with Amazon I have been able to reduce the amount of code needed considerably

no more need for an add rotator, the new ads will now do that automatically. so this simplifies the setup considerably

Now PHP included files can be smaller than every, which means better overall page load time improvements too

This JavaScript is what I was using to generate randomized ads so that I can tempt visitors

Code: Select all

// Fisher-Yates shuffle model
function shuffle(array) {
  	var m = array.length, t, i;
   	while (m) { // While there remain elements to shuffle ...
	    i = Math.floor(Math.random() * m--); // Pick a remaining element ...
	    t = array[m]; // And swap it with the current element...
	    array[m] = array[i];
	    array[i] = t;
	  }
  	return array;
}
can somebody show this capability in PHP?
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: less code needed

Post by Celauran »

User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: less code needed

Post by Christopher »

:drunk:
(#10850)
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: less code needed

Post by Vegan »

Do you know if the PHP implementation is as good as the algorithm I have?
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: less code needed

Post by Celauran »

https://github.com/php/php-src/blob/71c ... ng.c#L5440
(That's actually string shuffle, but it borrows from array shuffle and is shorter and easier to read, actual implementation code below)
https://github.com/php/php-src/blob/d88 ... ay.c#L2287

tl;dr Shuffling itself is pretty much the same, but will perform better since it's C code.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: less code needed

Post by Christopher »

I think shuffle uses rand() which is worse than Fisher-Yates. It is probably random enough for ads.
(#10850)
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: less code needed

Post by Vegan »

no question that some thinking about the logic used matters

consider a web app card game, which is what I originally envisioned using Fisher-Yates for as it can literally shuffle an array of cards etc

the rand() function is fine for a dice throw etc, but cards and adverts are not the same as a coin toss
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: less code needed

Post by Christopher »

Vegan wrote:fine for a dice throw etc, but cards and adverts are not the same as a coin toss
I would say not ok for dice throws, cards or coin tosses -- for actual games. Whether it is ok for adverts is a separate question.
(#10850)
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: less code needed

Post by Vegan »

Slot machine sites are a good example of considering the randomizer more carefully, always somebody looking for an edge

For adverts its not such a bad idea, but my script for shuffling is stronger when faced with a weaker rand() function

It was not until Visual C++ 2013 that std::random became a true random number, before that it was a pseudo random number

Ads are a different class and they are not sensitive to random number ambiguities, instead I only want ads to be shuffled so the page has a slightly better chance to get a bite

then there is that behavourial advertising but the may not fit the 20MB database they way I would like
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: less code needed

Post by Christopher »

Vegan wrote:a true random number, before that it was a pseudo random number
This is not an simple distinction between true and pseudo. There are many shades of gray in that spectrum depending on the quality of the algorithm, source of entropy, seeding, etc. It is important to understand what is appropriate for the problem being solved. That determines whether shuffle()/rand() is appropriate or whether to use mt_rand(), openssl, PHP7's random_int(), etc.
(#10850)
User avatar
Vegan
Forum Regular
Posts: 574
Joined: Fri Sep 05, 2008 3:34 pm
Location: Victoria, BC
Contact:

Re: less code needed

Post by Vegan »

Unfortunately Azure is slow to roll out PHP7, so I am using PHP 5.4 which is the current default

I use very little function calls, favoring my own code for whatever the need is
Hardcore Games™ Legendary is the Only Way to Play™
My site is powered by LAMP
Post Reply