Link to Random WordPress Post
copy $p = get_posts( array( 'orderby' => 'rand' , 'showposts' => 1 ) ) ; echo '<a href="' .get_permalink ( $p[ 0 ] -> ID) .'">Random</a>' ;
I have been wanting to add a link to the header of this site that will randomly open a snippet zone post… Finally got around to it the other day – figured I’d post the lazy php code for it 😀
Super Simple Seeded Random
copy let _seed = 1234567 ; // Deterministic pseudo-random float in the interval [ 0, 1 ] function seededRandom( s ) { if ( s !== undefined ) _seed = s % 2147483647 ; // Park-Miller algorithm _seed = _seed * 16807 % 2147483647 ; return ( _seed - 1 ) / 2147483646 ; }
Straight from the THREE.js source code – a fun and simple seeded random. The best libraries are always filled with gems like this…
If I were going to use this I would do a couple things out of pure preference:
copy let seed = 1234567 ; const setSeed = newSeed => seed = newSeedfunction seededRand( ) { // Park-Miller algorithm seed *= 16807 % 0x7fffffff; return ( seed - 1 ) / 0x7fffffff; } // try it out: console.log ( 'one' , seededRand( ) ) ; console.log ( 'two' , seededRand( ) ) ; console.log ( 'three' , seededRand( ) ) ; seed = 9999 console.log ( 'one new seed' , seededRand( ) ) ; console.log ( 'one new seed' , seededRand( ) ) ; seed = 1234567 ; console.log ( 'one old seed' , seededRand( ) ) ;
Try it out…
If you’re wondering about what this is doing… read more about it here .
Bring Node to Top
copy myEl.parentNode .appendChild ( myEl) ;
This will make an element the top most of all of its siblings…
copy document.body .innerHTML += ` < div> < button> one</ button> < button> two</ button> < button> three</ button> < button> four</ button> </ div> < style> button { display: block; cursor: pointer; } </ style> `; document.addEventListener ( 'click' , e => { if ( e.target .tagName === 'BUTTON' ) { e.target .parentNode .appendChild ( e.target ) ; } } ) ;
Try it out…
Clicking on any button will bring it to the top most position within its parent.
Bind All Methods to a Class instance JavaScript
copy function bindAll( target) { const keys = Object .getOwnPropertyNames ( target.constructor .prototype ) ; for ( let i = 0 ; i < keys.length ; i++ ) { const key = keys[ i] ; const propOrMethod = target[ key] ; if ( typeof propOrMethod === 'function' ) { target[ key] = target[ key] .bind ( target) ; } } } class Test { constructor( ) { bindAll( this ) ; document.addEventListener ( 'click' , this .onClick ) ; } onClick( e) { console.log ( 'click' , this ) ; } otherMethod( e) { console.log ( 'test' , this ) ; } } const test = new Test( ) ; setInterval( test.otherMethod , 1000 ) ;
Try it out…
This is useful when you know you need many methods of a given class to be bound to the classes instance. Another way to do this is to selectively use instance properties:
copy class Test { constructor( ) { document.addEventListener ( 'click' , this .onClick ) ; } onClick = e => { console.log ( 'click' , this ) ; } otherMethod e => { console.log ( 'test' , this ) ; } }
Avoiding classes is another way to not have to deal with this issue 😉
valueOf for Shorter Function Calls
copy r= { valueOf: _=> Math .random ( ) } console.log ( + r) console.log ( + r) console.log ( + r)
Try it out…
Use valueOf
to shorten function calls. I learned this trick over at stackexchange codegolf here from user cyoce .