Skip to content

Instantly share code, notes, and snippets.

@toastdriven
Created March 3, 2015 06:53
Show Gist options
  • Save toastdriven/45a03589731f0c2eac47 to your computer and use it in GitHub Desktop.
Save toastdriven/45a03589731f0c2eac47 to your computer and use it in GitHub Desktop.
Random (in Python, Javascript, Go & Swift)
// Remove Me
package main
// package random
import (
"fmt"
"math/rand"
"time"
)
type RandomItem interface{}
type Random struct {
seed int64
rng *rand.Rand
}
func (r *Random) Int(max int) int {
return r.rng.Intn(max)
}
func (r *Random) Choice(choices []RandomItem) RandomItem {
offset := r.Int(len(choices))
return choices[offset]
}
func (r *Random) Shuffle(choices []RandomItem) []RandomItem {
// Inefficient, because I can no longer give any fucks.
shuffled := choices
for i, _ := range shuffled {
offset := r.Int(len(shuffled))
tmp := shuffled[i]
shuffled[i] = shuffled[offset]
shuffled[offset] = tmp
}
return shuffled
}
func New() *Random {
seed := int64(time.Now().UnixNano())
return NewWithSeed(seed)
}
func NewWithSeed(seed int64) *Random {
source := rand.NewSource(seed)
rng := rand.New(source)
return &Random{seed, rng}
}
// Remove Me
func main() {
r := New()
fmt.Println("Int:", r.Int(32))
choices := []string{"a", "b", "c", "d", "e"}
grump := make([]RandomItem, len(choices))
for i := range choices {
grump[i] = RandomItem(choices[i])
}
fmt.Println("Choice:", r.Choice(grump))
fmt.Println("Shuffle:", r.Shuffle(grump))
}
function Random(seed) {
this.seed = seed || Math.floor(Math.random() * 1000000)
}
var cons = Random
, proto = cons.prototype
proto.random = function() {
var x = Math.sin(this.seed++) * 10000
return x - Math.floor(x)
}
proto.randint = function(start, end) {
var multiplier = end - start
var rand = this.random()
rand = Math.floor(rand * multiplier)
return rand + start
}
proto.choice = function(collection) {
var len = collection.length
var choice = this.randint(0, len)
return collection[choice]
}
proto.shuffle = function(collection) {
var len = collection.length
var offset, tmp
for(var i = 0; i < len; i++) {
offset = this.randint(0, len)
tmp = collection[i]
collection[i] = collection[offset]
collection[offset] = tmp
}
return collection
}
module.exports.Random = Random
// To demo...
var r = new Random()
console.log("Int:", r.randint(0, 32))
console.log("Choice:", r.choice(["a", "b", "c", "d", "e"]))
console.log("Shuffle:", r.shuffle(["a", "b", "c", "d", "e"]))
import random
print "Int:", random.randint(32)
print "Choice:", random.choice(['a', 'b', 'c', 'd', 'e'])
print "Shuffle:", random.shuffle(['a', 'b', 'c', 'd', 'e'])
import Foundation
class Random {
func int(num: Int) -> Int {
var converted = UInt32(num)
return Int(arc4random_uniform(converted))
}
func choice(choices: [Any]) -> Any {
var offset = self.int(choices.count)
return choices[offset]
}
func shuffle(choices: Array<Any>) -> Array<Any> {
// Copy it to produce a mutable version.
var copied = choices
var len = copied.count
// Half-open range, so we don't overflow the array length.
for i in 0..<len {
var roffset = self.int(len - i) + i
swap(&copied[i], &copied[roffset])
}
return copied
}
}
var r = Random()
print(r.int(12))
print(r.choice(["a", "b", "c", "d", "e"]))
print(r.shuffle(["a", "b", "c", "d", "e"]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment