Advertisement
Guest User

Untitled

a guest
Feb 5th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import Cocoa
  2.  
  3. class LinearCongruntialGenerator {
  4.  
  5. var state = 0 //seed of 0 by default
  6. let a, c, m, shift: Int
  7.  
  8. //we will use microsoft random by default
  9. init() {
  10. self.a = 214013
  11. self.c = 2531011
  12. self.m = Int(pow(2.0, 31.0)) //2^31 or 2147483648
  13. self.shift = 16
  14. }
  15.  
  16. init(a: Int, c: Int, m: Int, shift: Int) {
  17. self.a = a
  18. self.c = c
  19. self.m = m //2^31 or 2147483648
  20. self.shift = shift
  21. }
  22.  
  23. func seed(seed: Int) -> Void {
  24. state = seed;
  25. }
  26.  
  27. func random() -> Int {
  28. state = (a * state + c) % m
  29. return state >> shift
  30. }
  31. }
  32.  
  33. let microsoftLinearCongruntialGenerator = LinearCongruntialGenerator()
  34. let BSDLinearCongruntialGenerator = LinearCongruntialGenerator(a: 1103515245, c: 12345, m: 2147483648, shift: 0)
  35.  
  36. print("Microsft Rand:")
  37. for(var i = 0; i < 10; i++)
  38. {
  39. print(microsoftLinearCongruntialGenerator.random())
  40. }
  41.  
  42. print("") //new line for readability
  43. print("BSD Rand:")
  44. for(var i = 0; i < 10; i++)
  45. {
  46. print(BSDLinearCongruntialGenerator.random())
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement