prvhash.bmx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ' Copyright (c) 2023 Bruce A Henderson
  2. '
  3. ' Permission is hereby granted, free of charge, to any person obtaining a
  4. ' copy of this software and associated documentation files (the "Software"),
  5. ' to deal in the Software without restriction, including without limitation
  6. ' the rights to use, copy, modify, merge, publish, distribute, sublicense,
  7. ' and/or sell copies of the Software, and to permit persons to whom the
  8. ' Software is furnished to do so, subject to the following conditions:
  9. '
  10. ' The above copyright notice and this permission notice shall be included in
  11. ' all copies or substantial portions of the Software.
  12. '
  13. ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. ' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. ' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. ' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. ' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  19. ' DEALINGS IN THE SOFTWARE.
  20. '
  21. SuperStrict
  22. Rem
  23. bbdoc: Random Numbers - PRVHASH
  24. End Rem
  25. Module Random.PRVHASH
  26. ModuleInfo "Version: 1.00"
  27. ModuleInfo "License: MIT"
  28. ModuleInfo "Copyright: Wrapper - 2023 Bruce A Henderson"
  29. ModuleInfo "Copyright: PRVHASH - 2020-2023 Aleksey Vaneev"
  30. ModuleInfo "History: 1.00"
  31. ModuleInfo "History: Initial Release."
  32. Import Random.Core
  33. Import "prvhash/*.h"
  34. Import "glue.c"
  35. Type TPrvHashRandom Extends TRandom
  36. Private
  37. Field rnd_state:SHashState
  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_prvhash_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_prvhash_next_double(rnd_state)*(1:Double+Range) )+minValue
  59. Return Int( bmx_prvhash_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_prvhash_seed(ULong(seed), rnd_state)
  67. End Method
  68. Method RndSeed:Int()
  69. Return rnd_seed
  70. End Method
  71. Method GetName:String()
  72. Return "PRVHASH"
  73. End Method
  74. End Type
  75. Private
  76. Type TPrvHashRandomFactory Extends TRandomFactory
  77. Method New()
  78. Super.New()
  79. Init()
  80. End Method
  81. Method GetName:String()
  82. Return "PRVHASH"
  83. End Method
  84. Method Create:TRandom(seed:Int)
  85. Return New TPrvHashRandom(seed)
  86. End Method
  87. Method Create:TRandom()
  88. Return New TPrvHashRandom()
  89. End Method
  90. End Type
  91. Struct SHashState
  92. Field seed:ULong
  93. Field lcg:ULong
  94. Field hash:ULong
  95. End Struct
  96. Extern
  97. Function bmx_prvhash_seed(seed:ULong, state:SHashState Var)
  98. Function bmx_prvhash_next:ULong(state:SHashState Var)
  99. Function bmx_prvhash_next_double:Double(state:SHashState Var)
  100. End Extern
  101. New TPrvHashRandomFactory