* Returns an array of random values. The length of the array is numInts. Each * int will be >= 0 and < maxVal. * @param {!number} numInts * @param {!number} maxVal
(numInts, maxVal)
| 3098 | * @param {!number} maxVal |
| 3099 | */ |
| 3100 | function getRandomInts(numInts, maxVal) { |
| 3101 | // Ensure array type is appropriate for the max value (performance) |
| 3102 | const arr = |
| 3103 | maxVal < 256 |
| 3104 | ? new Uint8Array(numInts) |
| 3105 | : maxVal < 32768 |
| 3106 | ? new Uint16Array(numInts) |
| 3107 | : new Uint32Array(numInts); |
| 3108 | |
| 3109 | const isIE = !!self['msCrypto']; |
| 3110 | const localCrypto = isIE ? self['msCrypto'] : self.crypto; |
| 3111 | if (localCrypto && localCrypto.getRandomValues) { |
| 3112 | localCrypto.getRandomValues(arr); |
| 3113 | for (let i = arr.length - 1; i > -1; i--) { |
| 3114 | arr[i] = arr[i] % maxVal; |
| 3115 | } |
| 3116 | } else { |
| 3117 | // For older browsers |
| 3118 | for (let i = arr.length - 1; i > -1; i--) { |
| 3119 | arr[i] = Math.floor(Math.random() * maxVal); |
| 3120 | } |
| 3121 | } |
| 3122 | |
| 3123 | return arr; |
| 3124 | } |
| 3125 | |
| 3126 | /** |
| 3127 | * Copyright 2018 The Subscribe with Google Authors. All Rights Reserved. |