Last active
August 29, 2015 14:07
-
-
Save madr/e79c5691bf7608e31e86 to your computer and use it in GitHub Desktop.
Comparison of getting random values in different languages
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
(function () { | |
"use strict"; | |
var v, bandMembers = ["Hansi", "Andre", "Marcus", "Thomen"]; | |
// 1. one random number between 1 and 10 | |
v = Math.ceil(Math.random() * 10); | |
// 2: one random member of Blind Guardian | |
v = bandMembers[Math.ceil(Math.random() * (bandMembers.length - 1))]; | |
// 3: Two random members of Blind Guardian | |
v = (function (array) { | |
var currentIndex = array.length, temporaryValue, randomIndex; | |
while (0 !== currentIndex) { | |
randomIndex = Math.floor(Math.random() * currentIndex); | |
currentIndex -= 1; | |
temporaryValue = array[currentIndex]; | |
array[currentIndex] = array[randomIndex]; | |
array[randomIndex] = temporaryValue; | |
} | |
return array; | |
}(bandMembers)).slice(0, 2); | |
}()); |
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
<?php | |
$band_members = array("Hansi", "Andre", "Marcus", "Thomen"); | |
// 1. one random number between 1 and 10 | |
$v = rand(1, 10); | |
// 2: one random member of Blind Guardian | |
$v = array_rand($band_members); | |
// 3: Two random members of Blind Guardian | |
$v = array_rand($band_members, 2); |
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
import random | |
band_members = ["Hansi", "Andre", "Marcus", "Thomen"] | |
# 1. one random number between 1 and 10 | |
v = random.choice(range(1, 11)) | |
# 2: one random member of Blind Guardian | |
v = random.choice(band_members) | |
# 3: Two random members of Blind Guardian | |
v = random.sample(band_members) |
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
band_members = ["Hansi", "Andre", "Marcus", "Thomen"] | |
# 1. one random number between 1 and 10 | |
v = rand(1..10) | |
# 2: one random member of Blind Guardian | |
v = band_members.sample | |
# 3: Two random members of Blind Guardian | |
v = band_members.sample 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment