Get complete RandomSeed

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

Get complete RandomSeed

Post by oryaaaaa »

Hello.
Is the number biased? Yes. I have a nice idea.

Required Broadwell, Skylake, KabyLake, CoffeeLake ...

Code: Select all

; from wikipedia
; The RDSEED instruction was added to Intel Secure Key
; For seeding another pseudorandom number generator, available in Broadwell CPUs. 
Global RDSEED.i
!XOR Rax, Rax
!XOR Rdx, Rdx
!XOR Rbx, Rbx
!XOR Rcx, Rcx
!INC Eax
!INC Eax
!INC Eax
!INC Eax
!INC Eax
!INC Eax
!INC Eax ;7
!CPUID
!XOR Rax, Rax
!XOR Rdx, Rdx
!XOR Rcx, Rcx
!INC Rdx
!INC Rcx
!SHL Rdx, cl
!SHL Rdx, cl
!SHL Rdx, cl
!SHL Rdx, cl
!SHL Rdx, cl ; 5
!SHL Rdx, cl
!SHL Rdx, cl
!SHL Rdx, cl
!SHL Rdx, cl
!SHL Rdx, cl ; 10
!SHL Rdx, cl
!SHL Rdx, cl
!SHL Rdx, cl
!SHL Rdx, cl
!SHL Rdx, cl ; 15
!SHL Rdx, cl ; 16
!SHL Rdx, cl ; 17
!SHL Rdx, cl ; 18
!AND Ebx, Edx
!CMP Ebx, Edx
!JNE not_support_RDSEED
!INC Rax
!not_support_RDSEED:
!MOV [v_RDSEED], Rax
;
!XOR Rax, Rax
!XOR Rdx, Rdx
!XOR Rbx, Rbx
!XOR Rcx, Rcx
If Not RDSEED
  MessageRequester("ERROR", "This CPU can't support RDSEED.", #PB_MessageRequester_Ok|#PB_MessageRequester_Error)
  End
EndIf

Procedure Test()
  Protected Random_seed.i
  !XOR Rax, Rax
  !MOVNTI [p.v_Random_seed], Rax
  !RDSEED Rax
  !MOVNTI [p.v_Random_seed], Rax
  RandomSeed(Random_seed)
EndProcedure

Test()

; OR

Global Random_seed.i
!XOR Rax, Rax
!MOVNTI [v_Random_seed], Rax
!RDSEED Rax
!MOVNTI [v_Random_seed], Rax
RandomSeed(Random_seed)
User avatar
RSBasic
Moderator
Moderator
Posts: 1228
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: Get complete RandomSeed

Post by RSBasic »

I tested with my laptop. I get the following error message:
ERROR wrote:This CPU can't support RDSEED.
My processor is Intel i3-2310M.

I'm testing my desktop PC tonight. (Intel i5 2500K)
Image
Image
Post Reply