Last active
July 13, 2016 09:02
-
-
Save lankaapura/fbd85f4f23535dc4103b7b5c0c13d1ba to your computer and use it in GitHub Desktop.
generate random binary array and Inverse
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ar = getRandomArrayWithInverse(5); | |
console.log(ar.array); | |
console.log(ar.reverse); | |
function getRandomArrayWithInverse(size) { | |
var randomArray = []; | |
var inversedArray = []; | |
for (index = 0; index < size; index++) { | |
var random = getRandomArbitrary(); | |
randomArray.push(random); | |
inversedArray.push(random ^ 1); | |
} | |
return { | |
array: randomArray, | |
reverse: inversedArray | |
}; | |
} | |
function getRandomArray(size) { | |
var randomArray = []; | |
for (index = 0; index < size; index++) { | |
randomArray.push(getRandomArbitrary()); | |
} | |
return randomArray; | |
} | |
function getRandomArbitrary() { | |
return Math.floor(Math.random() * (1 - 0 + 1)) + 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ar = getRandomArray(5); | |
var ar2 = inverseArray(ar) | |
alert(ar) | |
alert(ar2) | |
function inverseArray(array) { | |
var inversed = []; | |
array.forEach(function(i) { | |
inversed.push(i ^ 1); | |
}); | |
return inversed; | |
} | |
function getRandomArray(size) { | |
var randomArray = []; | |
for (index = 0; index < size; index++) { | |
randomArray.push(getRandomArbitrary()); | |
} | |
return randomArray; | |
} | |
function getRandomArbitrary() { | |
return Math.floor(Math.random() * (1 - 0 + 1)) + 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ar = [1, 0, 0]; | |
var ar2 = [] | |
ar.forEach(function(i) { | |
ar2.push(i^1); | |
}); | |
alert(ar) | |
alert(ar2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment