Page 1 of 1

less code needed

Posted: Thu Jan 21, 2016 8:11 pm
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?

Re: less code needed

Posted: Thu Jan 21, 2016 8:39 pm
by Celauran

Re: less code needed

Posted: Thu Jan 21, 2016 11:25 pm
by Christopher
:drunk:

Re: less code needed

Posted: Sat Jan 23, 2016 3:13 pm
by Vegan
Do you know if the PHP implementation is as good as the algorithm I have?

Re: less code needed

Posted: Sat Jan 23, 2016 3:40 pm
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.

Re: less code needed

Posted: Sat Jan 23, 2016 7:29 pm
by Christopher
I think shuffle uses rand() which is worse than Fisher-Yates. It is probably random enough for ads.

Re: less code needed

Posted: Fri Feb 26, 2016 8:06 pm
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

Re: less code needed

Posted: Fri Feb 26, 2016 9:46 pm
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.

Re: less code needed

Posted: Sat Feb 27, 2016 6:02 am
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

Re: less code needed

Posted: Sat Feb 27, 2016 8:02 pm
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.

Re: less code needed

Posted: Sun Feb 28, 2016 6:29 pm
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