Skip to content

Instantly share code, notes, and snippets.

@lankaapura
Last active July 13, 2016 09:02
Show Gist options
  • Save lankaapura/fbd85f4f23535dc4103b7b5c0c13d1ba to your computer and use it in GitHub Desktop.
Save lankaapura/fbd85f4f23535dc4103b7b5c0c13d1ba to your computer and use it in GitHub Desktop.
generate random binary array and Inverse
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;
}
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;
}
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