forked from github/game-off-2013
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utility.js
45 lines (39 loc) · 1.15 KB
/
utility.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"use strict";
define([], function() {
function random(max) {
return Math.floor( Math.random()*max );
}
function randomElement( array ) {
return array[ random(array.length-1) ];
}
function randomZero(max) {
return Math.floor( Math.random()*max-max/2 );
}
function noteToFrequency( noteNumber ) {
return Math.pow(2, ( noteNumber - 69 ) / 12) * 440;
}
function rotateArray(array,n) {
var result = array.slice();
result.unshift.apply( result, result.splice( -n, result.length ) );
return result;
}
//http://stackoverflow.com/a/12646864
function shuffleArray(arr) {
var array = arr.slice();
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
return {
random: random,
randomZero: randomZero,
randomElement: randomElement,
noteToFrequency: noteToFrequency,
shuffleArray: shuffleArray,
rotateArray: rotateArray
};
});