|
| 1 | + |
| 2 | +/** |
| 3 | +* I return a random float between 0 (inclusive) and 1 (exclusive) using the Math module. |
| 4 | +*/ |
| 5 | +function randFloatWithMath() { |
| 6 | + |
| 7 | + return Math.random(); |
| 8 | + |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | +* I return a random float between 0 (inclusive) and 1 (exclusive) using the Crypto module. |
| 13 | +*/ |
| 14 | +function randFloatWithCrypto() { |
| 15 | + |
| 16 | + // This method works by filling the given array with random values of the given type. |
| 17 | + // In our case, we only need one random value, so we're going to pass in an array of |
| 18 | + // length 1. |
| 19 | + // -- |
| 20 | + // Note: For better performance, we could cache the typed array and just keep passing |
| 21 | + // the same reference in (cuts performance in half). But, we're exploring the |
| 22 | + // randomness, not the performance. |
| 23 | + var [ randomInt ] = crypto.getRandomValues( new Uint32Array( 1 ) ); |
| 24 | + var maxInt = 4294967295; |
| 25 | + |
| 26 | + // Unlike Math.random(), crypto is giving us an integer. To feed this back into the |
| 27 | + // same kind of math equation, we have to convert the integer into decimal so that we |
| 28 | + // can figure out where in our range our randomness leads us. |
| 29 | + return ( randomInt / ( maxInt + 1 ) ); |
| 30 | + |
| 31 | +} |
| 32 | + |
| 33 | +// ----------------------------------------------------------------------------------- // |
| 34 | +// ----------------------------------------------------------------------------------- // |
| 35 | + |
| 36 | +function Explore( algorithm ) { |
| 37 | + |
| 38 | + // Each instance of this Alpine.js component is assigned a different randomization |
| 39 | + // strategy for floats (0..1). Other than that, the component instances behave exactly |
| 40 | + // the same. |
| 41 | + var randFloat = ( algorithm === "math" ) |
| 42 | + ? randFloatWithMath |
| 43 | + : randFloatWithCrypto |
| 44 | + ; |
| 45 | + |
| 46 | + return { |
| 47 | + duration: 0, |
| 48 | + // Public methods. |
| 49 | + init: init, |
| 50 | + // Private methods. |
| 51 | + fillCanvas: fillCanvas, |
| 52 | + fillList: fillList, |
| 53 | + randRange: randRange |
| 54 | + } |
| 55 | + |
| 56 | + // --- |
| 57 | + // PUBLIC METHODS. |
| 58 | + // --- |
| 59 | + |
| 60 | + /** |
| 61 | + * I initialize the Alpine.js component. |
| 62 | + */ |
| 63 | + function init() { |
| 64 | + |
| 65 | + var startedAt = Date.now(); |
| 66 | + |
| 67 | + this.fillCanvas(); |
| 68 | + this.fillList(); |
| 69 | + |
| 70 | + this.duration = ( Date.now() - startedAt ); |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + // --- |
| 75 | + // PRIVATE METHODS. |
| 76 | + // --- |
| 77 | + |
| 78 | + /** |
| 79 | + * I populate the canvas with random {X,Y} pixels. |
| 80 | + */ |
| 81 | + function fillCanvas() { |
| 82 | + |
| 83 | + var pixelCount = 200000; |
| 84 | + var canvas = this.$refs.canvas; |
| 85 | + var width = canvas.width; |
| 86 | + var height = canvas.height; |
| 87 | + |
| 88 | + var context = canvas.getContext( "2d" ); |
| 89 | + context.fillStyle = "deeppink"; |
| 90 | + |
| 91 | + for ( var i = 0 ; i < pixelCount ; i++ ) { |
| 92 | + |
| 93 | + var x = this.randRange( 0, width ); |
| 94 | + var y = this.randRange( 0, height ); |
| 95 | + |
| 96 | + // As we add more pixels, let's make the pixel colors increasingly opaque. I |
| 97 | + // was hoping that this might help show potential clustering of values. |
| 98 | + context.globalAlpha = ( i / pixelCount ); |
| 99 | + context.fillRect( x, y, 1, 1 ); |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * I populate the list with random 0-9 values. |
| 107 | + */ |
| 108 | + function fillList() { |
| 109 | + |
| 110 | + var list = this.$refs.list; |
| 111 | + var valueCount = 105; |
| 112 | + var values = []; |
| 113 | + |
| 114 | + for ( var i = 0 ; i < valueCount ; i++ ) { |
| 115 | + |
| 116 | + values.push( this.randRange( 0, 9 ) ); |
| 117 | + |
| 118 | + } |
| 119 | + |
| 120 | + list.textContent = values.join( " " ); |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * I generate a random integer in between the given min and max, inclusive. |
| 126 | + */ |
| 127 | + function randRange( min, max ) { |
| 128 | + |
| 129 | + return ( min + Math.floor( randFloat() * ( max - min + 1 ) ) ); |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments