Created
October 24, 2016 13:46
-
-
Save ysnrkdm/5cfc26c7ed8b9420cee824f6e3dfc118 to your computer and use it in GitHub Desktop.
Original iOS application before separating library
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
// | |
// RandomGeneratorXor128.swift | |
// EFTest | |
// | |
// Created by KodamaYoshinori on 2016/10/22. | |
// Copyright © 2016 Yoshinori Kodama. All rights reserved. | |
// | |
class RandomGeneratorXor128 { | |
static var seed: Int = 19937 | |
static var generator: () -> RandomGeneratorXor128 = { | |
return RandomGeneratorXor128(seed: RandomGeneratorXor128.seed) | |
} | |
var seed: UInt64 | |
var x: UInt64 | |
var y: UInt64 | |
var z: UInt64 | |
var w: UInt64 | |
private init(seed: Int) { | |
self.seed = UInt64.init(abs(seed)) | |
self.x = 123456789 | |
self.y = 362436069 | |
self.z = 521288629 | |
self.w = 88675123 | |
// print("seed used: \(seed)") | |
self.z ^= self.seed | |
self.z ^= z >> 21; z ^= z << 35; z ^= z >> 4 | |
self.z = self.z &* 2685821657736338717 | |
// print("and z is initialized to: \(z)") | |
} | |
func xor128() -> UInt64 { | |
let t = x ^ (x << 11) | |
x = y; y = z; z = w; | |
w = (w ^ (w >> 19)) ^ (t ^ (t >> 8)) | |
// print("\(x), \(y), \(z), \(w)") // debug print | |
return w | |
} | |
func getNextRandomUInt() -> UInt { | |
return UInt.init(truncatingBitPattern: xor128()) | |
} | |
func getNextRandomInt() -> Int { | |
return Int.init(truncatingBitPattern: xor128()) | |
} | |
} |
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
// | |
// ViewController.swift | |
// EF_Test | |
// | |
// Created by KodamaYoshinori on 2016/10/24. | |
// Copyright © 2016 Yoshinori Kodama. All rights reserved. | |
// | |
import UIKit | |
class ViewController: UIViewController { | |
@IBOutlet weak var randomButton: UIButton! | |
var randHelper: RandomGeneratorXor128? = nil | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
let seed = Date().hashValue | |
RandomGeneratorXor128.seed = seed | |
print("Seed set: \(seed)") | |
randHelper = RandomGeneratorXor128.generator() | |
_ = randHelper!.getNextRandomInt() | |
_ = randHelper!.getNextRandomInt() | |
_ = randHelper!.getNextRandomInt() | |
_ = randHelper!.getNextRandomInt() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
@IBAction func randomButtonPressed(_ sender: AnyObject) { | |
let rand = randHelper!.getNextRandomUInt() | |
randomButton.setTitle("\(rand)", for: .normal) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment