Created
October 12, 2022 02:11
-
-
Save ZaidaZadkiel/00b560afe0e79ebf21cd986afcb87af9 to your computer and use it in GitHub Desktop.
Take two entries from an array which are never the same entry
This file contains hidden or 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
function get_two_random_elements() { | |
let indexA = ( Math.floor(Math.random() * data.length ) ) | |
let indexB = ( (Math.floor(Math.random() * (data.length-1)) + indexA + 1) % data.length ) | |
console.log( | |
`${indexA}: ${data[indexA]}, ${indexB}: ${data[indexB]}`, | |
indexA == indexB ? "SAME" : "ok", | |
data.length | |
) | |
} | |
/* | |
test: | |
let data = ["A", "B", "C", "D", "E", "F"] | |
for(let i = 0; i!= 100; i++){ get_two_random_elements() } | |
output: | |
0: A, 2: C ok 6 | |
5: F, 4: E ok 6 | |
4: E, 3: D ok 6 | |
2: C, 3: D ok 6 | |
0: A, 1: B ok 6 | |
3: D, 0: A ok 6 | |
2: C, 4: E ok 6 | |
0: A, 4: E ok 6 | |
1: B, 4: E ok 6 | |
2: C, 1: B ok 6 | |
0: A, 1: B ok 6 | |
4: E, 3: D ok 6 | |
2: C, 1: B ok 6 | |
0: A, 5: F ok 6 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment