Rnd: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
mNo edit summary
mNo edit summary
Line 3: Line 3:


==Description ==
==Description ==
Gets a random number within a range with the RtlGenRandom Api.
Generates a random number within a range using the RtlGenRandom api.


== Example ==
== Example ==
Line 22: Line 22:
== Functions ==
== Functions ==
<highlight-nsis>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
<highlight-nsis>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Gets a random number within a range with the RtlGenRandom Api
;; Gets a random number within a range with the RtlGenRandom api
;; P1 :out: Random number
;; P1 :out: Random number
;; P2 :in:  Minimum value
;; P2 :in:  Minimum value
Line 46: Line 46:
   IntOp $3 $3 + 1
   IntOp $3 $3 + 1
   System::Call '*(i) i .r4'
   System::Call '*(i) i .r4'
   System::Call 'advapi32::SystemFunction036(i r4, i 4)'
   System::Call 'advapi32::SystemFunction036(i r4, i 4)' ;; RtlGenRandom
   System::Call '*$4(i .r1)'
   System::Call '*$4(i .r1)'
   System::Free $4
   System::Free $4
   IntOp $1 $1 & 0x7FFFFFFF
   IntOp $1 $1 & 0x7FFFFFFF ;; eliminate possible negative value
   IntOp $1 $1 % $3
   IntOp $1 $1 % $3 ;; fit the value within the range
   IntOp $0 $1 + $0
   IntOp $0 $1 + $0 ;; index it with the minimum value
    
    
   Pop $4
   Pop $4
Line 59: Line 59:
FunctionEnd</highlight-nsis>
FunctionEnd</highlight-nsis>
<highlight-nsis>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
<highlight-nsis>;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Gets a random number within a specific range with the RtlGenRandom Api (Int64)
;; Gets a random number within a range with the RtlGenRandom api (Int64)
;; P1 :out: Random number
;; P1 :out: Random number
;; P2 :in:  Minimum value
;; P2 :in:  Minimum value
Line 85: Line 85:
   Pop $3
   Pop $3
   System::Call '*(l) i .r4'
   System::Call '*(l) i .r4'
   System::Call 'advapi32::SystemFunction036(i r4, i 8)'
   System::Call 'advapi32::SystemFunction036(i r4, i 8)' ;; RtlGenRandom
   System::Call '*$4(l .r1)'
   System::Call '*$4(l .r1)'
   System::Free $4
   System::Free $4
   System::Int64Op $1 & 0x7FFFFFFFFFFFFFFF
   System::Int64Op $1 & 0x7FFFFFFFFFFFFFFF ;; eliminate possible negative value
   Pop $1
   Pop $1
   System::Int64Op $1 % $3
   System::Int64Op $1 % $3 ;; fit the value within the range
   Pop $1
   Pop $1
   System::Int64Op $1 + $0
   System::Int64Op $1 + $0 ;; index it with the minimum value
   Pop $0
   Pop $0
    
    

Revision as of 21:17, 2 December 2011

Author: Lloigor (talk, contrib)


Description

Generates a random number within a range using the RtlGenRandom api.

Example

!define tempfile "$EXEDIR\test_output.txt"
 
FileOpen $R0 "${tempfile}" "w"
StrCpy $3 0
loop:
   IntOp $3 $3 + 1
;--------------------------
   ${Rnd} $0 -50000 50000
;--------------------------
   FileWrite $R0 "$0$\r$\n"
   IntCmp $3 1000 0 loop
FileClose $R0
ExecWait 'notepad "${tempfile}"'
Delete "${tempfile}"

Functions

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Gets a random number within a range with the RtlGenRandom api
;; P1 :out: Random number
;; P2 :in:  Minimum value
;; P3 :in:  Maximum value
;; min/max P2 and P3 values = -2 147 483 647 / 2 147 483 647
;; max interval = 2 147 483 647
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
!define Rnd "!insertmacro _Rnd"
!macro _Rnd _RetVal_ _Min_ _Max_
   Push "${_Max_}"
   Push "${_Min_}"
   Call Rnd
   Pop ${_RetVal_}
!macroend
Function Rnd
   Exch $0  ;; Min / return value
   Exch
   Exch $1  ;; Max / temp var
   Push "$3"  ;; difference between Max and Min
   Push "$4"  ;; random value
 
   IntOp $3 $1 - $0
   IntOp $3 $3 + 1
   System::Call '*(i) i .r4'
   System::Call 'advapi32::SystemFunction036(i r4, i 4)'  ;; RtlGenRandom
   System::Call '*$4(i .r1)'
   System::Free $4
   IntOp $1 $1 & 0x7FFFFFFF  ;; eliminate possible negative value
   IntOp $1 $1 % $3  ;; fit the value within the range
   IntOp $0 $1 + $0  ;; index it with the minimum value
 
   Pop $4
   Pop $3
   Pop $1
   Exch $0
FunctionEnd
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Gets a random number within a range with the RtlGenRandom api (Int64)
;; P1 :out: Random number
;; P2 :in:  Minimum value
;; P3 :in:  Maximum value
;; min/max P2 and P3 values = -9 223 372 036 854 775 807 / 9 223 372 036 854 775 807
;; max interval = 9 223 372 036 854 775 807
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
!define Rnd64 "!insertmacro _Rnd64"
!macro _Rnd64 _RetVal_ _Min_ _Max_
   Push "${_Max_}"
   Push "${_Min_}"
   Call Rnd64
   Pop ${_RetVal_}
!macroend
Function Rnd64
   Exch $0  ;; Min / return value
   Exch
   Exch $1  ;; Max / temp var
   Push "$3"  ;; difference between Max and Min
   Push "$4"  ;; random value
 
   System::Int64Op $1 - $0
   Pop $3
   System::Int64Op $3 + 1
   Pop $3
   System::Call '*(l) i .r4'
   System::Call 'advapi32::SystemFunction036(i r4, i 8)'  ;; RtlGenRandom
   System::Call '*$4(l .r1)'
   System::Free $4
   System::Int64Op $1 & 0x7FFFFFFFFFFFFFFF  ;; eliminate possible negative value
   Pop $1
   System::Int64Op $1 % $3  ;; fit the value within the range
   Pop $1
   System::Int64Op $1 + $0  ;; index it with the minimum value
   Pop $0
 
   Pop $4
   Pop $3
   Pop $1
   Exch $0
FunctionEnd