xoshiro.bmx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. ' Copyright (c) 2020 Bruce A Henderson
  2. '
  3. ' This software is provided 'as-is', without any express or implied
  4. ' warranty. In no event will the authors be held liable for any damages
  5. ' arising from the use of this software.
  6. '
  7. ' Permission is granted to anyone to use this software for any purpose,
  8. ' including commercial applications, and to alter it and redistribute it
  9. ' freely, subject to the following restrictions:
  10. '
  11. ' 1. The origin of this software must not be misrepresented; you must not
  12. ' claim that you wrote the original software. If you use this software
  13. ' in a product, an acknowledgment in the product documentation would be
  14. ' appreciated but is not required.
  15. '
  16. ' 2. Altered source versions must be plainly marked as such, and must not be
  17. ' misrepresented as being the original software.
  18. '
  19. ' 3. This notice may not be removed or altered from any source
  20. ' distribution.
  21. '
  22. SuperStrict
  23. Rem
  24. bbdoc: Random Numbers - Xoshiro
  25. End Rem
  26. Module Random.Xoshiro
  27. ModuleInfo "Version: 1.00"
  28. ModuleInfo "License: zlib"
  29. ModuleInfo "Copyright: Wrapper - 2020 Bruce A Henderson"
  30. ModuleInfo "Copyright: xoshiro256++ - 2019 David Blackman and Sebastiano Vigna"
  31. ModuleInfo "History: 1.00"
  32. ModuleInfo "History: Initial Release."
  33. Import Random.Core
  34. Import "src/xoshiro256plusplus.c"
  35. Type TXoshiroRandom Extends TRandom
  36. Private
  37. Field rnd_state:SState
  38. Field rnd_seed:Int
  39. Public
  40. Method New()
  41. SeedRnd(GenerateSeed())
  42. End Method
  43. Method New(seed:Int)
  44. SeedRnd seed
  45. End Method
  46. Method RndFloat:Float()
  47. Return Float(RndDouble())
  48. End Method
  49. Method RndDouble:Double()
  50. Return bmx_xoshiro_next_double(rnd_state)
  51. End Method
  52. Method Rnd:Double(minValue:Double = 1, maxValue:Double = 0)
  53. If maxValue > minValue Return RndDouble() * (maxValue - minValue) + minValue
  54. Return RndDouble() * (minValue - maxValue) + maxValue
  55. End Method
  56. Method Rand:Int(minValue:Int, maxValue:Int = 1)
  57. Local Range:Double = maxValue - minValue
  58. If Range > 0 Return Int( bmx_xoshiro_next_double(rnd_state)*(1:Double+Range) )+minValue
  59. Return Int( bmx_xoshiro_next_double(rnd_state)*(1:Double-Range) )+maxValue
  60. End Method
  61. Method SeedRnd(seed:Int)
  62. rnd_seed = seed
  63. If seed = 0 Then
  64. seed = $1234
  65. End If
  66. bmx_xoshiro_seed(ULong(seed), rnd_state)
  67. End Method
  68. Method RndSeed:Int()
  69. Return rnd_seed
  70. End Method
  71. End Type
  72. Private
  73. Type TXoshiroRandomFactory Extends TRandomFactory
  74. Method New()
  75. Super.New()
  76. Init()
  77. End Method
  78. Method GetName:String()
  79. Return "Xoshiro"
  80. End Method
  81. Method Create:TRandom(seed:Int)
  82. Return New TXoshiroRandom(seed)
  83. End Method
  84. Method Create:TRandom()
  85. Return New TXoshiroRandom()
  86. End Method
  87. End Type
  88. Struct SState
  89. Field StaticArray rnd_state:ULong[4]
  90. End Struct
  91. Extern
  92. Function bmx_xoshiro_seed(seed:ULong, state:SState Var)
  93. Function bmx_xoshiro_next:ULong(state:SState Var)
  94. Function bmx_xoshiro_next_double:Double(state:SState Var)
  95. End Extern
  96. New TXoshiroRandomFactory