hardware generated Random value functions

Share your advanced PureBasic knowledge/code with the community.
User avatar
oryaaaaa
Addict
Addict
Posts: 825
Joined: Mon Jan 12, 2004 11:40 pm
Location: Okazaki, JAPAN

hardware generated Random value functions

Post by oryaaaaa »

Random functions depend RDRAND
  • Integer: -9223372036854775808 to +9223372036854775807
  • Integer: -Custom to + Custom
  • Double: -1.00 to + 1.00
  • Float: -1.00 to + 1.00
RDRAND r64
Read a 64-bit random number and store in the destination register.

Loads a hardware generated random value and store it in the destination register.
DEST[63:0] ← HW_RND_GEN.data

Code: Select all

Procedure.i cpuRandomMAX()
  ; "Integer -9223372036854775808 to +9223372036854775807"
  !MOVQ R8, xmm0 ; Bit Stabizer and CPU voltage UP
  !RDRAND R8
  !MOV Rax, R8
  ProcedureReturn
EndProcedure

Procedure.i cpuRandomCustom(cpuRandom_Maximum.i)
  Shared cpuRandom9223372036854775807.i
  ; "Integer -Custom to + Custom"
  Protected cpuRandom_Result.i
  !MOVQ R8, xmm0 ; Bit Stabizer and CPU voltage UP
  !RDRAND R8
  !MOV QWORD [p.v_cpuRandom_Result], R8
  ProcedureReturn IntQ(cpuRandom_Result / cpuRandom9223372036854775807 * (cpuRandom_Maximum) )
EndProcedure

Procedure.d cpuRandomDouble()
  Shared cpuRandom9223372036854775807.i
  ; "Double -1.00 to + 1.00"
  Protected cpuRandom_Result.i
  !MOVQ R8, xmm0 ; Bit Stabizer and CPU voltage UP
  !RDRAND R8
  !MOV QWORD [p.v_cpuRandom_Result], R8
  ProcedureReturn cpuRandom_Result / cpuRandom9223372036854775807
EndProcedure

Procedure.f cpuRandomFloat()
  Shared cpuRandom9223372036854775807.i
  ; "Float -1.00 to + 1.00"
  Protected cpuRandom_Result.i
  !MOVQ R8, xmm0 ; Bit Stabizer and CPU voltage UP
  !RDRAND R8
  !MOV QWORD [p.v_cpuRandom_Result], R8
  ProcedureReturn cpuRandom_Result / cpuRandom9223372036854775807
EndProcedure

Procedure.i cpuRandomCPUID()
  Shared cpuRandom9223372036854775807.i
  Protected cpuRandomCPUID.l
  !MOV R8d, 1
  !SHL R8d, 30
  !MOV Eax, 1
  !CPUID
  !AND R8d, Ecx
  !MOV dword [p.v_cpuRandomCPUID], R8d
  If cpuRandomCPUID>0
    ; v_cpuRandom9223372036854775807 = 9223372036854775807
    !MOV Rcx, 0
    !MOV R8, 0
    !INC cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !SHL R8, cl
    !INC R8b
    !MOVNTI QWORD [v_cpuRandom9223372036854775807], R8
    !NOP QWORD  [Rip]
    !NOP QWORD  [v_cpuRandom9223372036854775807]
    !MOV Rax, 0
    !INC al
    ProcedureReturn ; #True
  Else
    !MOV Rax, 0
    ProcedureReturn ; #False
  EndIf
EndProcedure

Global No.i

If cpuRandomCPUID()
  
  Debug "Integer -9223372036854775808 to +9223372036854775807"
  For No=1 To 10
    Debug cpuRandomMAX()
  Next
  
  Debug "Integer -Custom to + Custom"
  For No=1 To 10
    Debug Str(cpuRandomCustom(100))
  Next
  
  Debug "Double -1.00 to + 1.00"
  For No=1 To 10
    Debug StrD(cpuRandomDouble())
  Next
  
  Debug "Float -1.00 to + 1.00"
  For No=1 To 10
    Debug StrF(cpuRandomFloat())
  Next
  Debug ""
  
  ;   Debug ""
  ;   TestCycle = 1000000
  ;   Debug ""
  ;   Debug "Benchmark"
  ;   ElapsedMilliseconds_temp = ElapsedMilliseconds()
  ;   Delay(200)
  ;   For No=1 To TestCycle
  ;     cpuRandomMAX()
  ;     cpuRandomCustom(100)
  ;     cpuRandomDouble()
  ;     cpuRandomFloat()
  ;   Next
  ;   Debug ElapsedMilliseconds() - ElapsedMilliseconds_temp
  ;   Debug ""
  ;   TestCycle * 6
  ;   Debug ""
  ;   Debug "Benchmark"
  ;   ElapsedMilliseconds_temp = ElapsedMilliseconds()
  ;   Delay(200)
  ;   For No=1 To TestCycle
  ;     cpuRandomMAX()
  ;     cpuRandomCustom(100)
  ;     cpuRandomDouble()
  ;     cpuRandomFloat()
  ;   Next
  ;   Debug ElapsedMilliseconds() - ElapsedMilliseconds_temp
  ;   Debug ""
  
EndIf

End
miso
Enthusiast
Enthusiast
Posts: 404
Joined: Sat Oct 21, 2023 4:06 pm
Location: Hungary

Re: hardware generated Random value functions

Post by miso »

Hello! Can I ask what the specifications are? The cpuRandomCPUID returns 0 for me in this example, so it does not execute the rest of the code. I belive it's for asm backend x86 only, I might be wrong. I'm not that talented in asm, but cpuid is supported by my processor. (I only use PBx64 on Windows)
Post Reply