Generate a random number

From NSIS Wiki
Jump to navigationJump to search
Author: Afrow UK (talk, contrib)


Description

This is a simple script to generate a number between 0 and 9.

Usage

Call GenRanNum
Pop $R0

The Function

Function GenRanNum
Push $R0
Push $R1
Push $R2
GetTempFileName $R0
GetFileTime $R0 $R1 $R2
 StrLen $R1 $R2
 StrCpy $R1 $R2 1 -$R1
  StrCmp $R1 - 0 +5
 StrLen $R1 $R2
 IntOp $R1 $R1 - 1
 StrCpy $R2 $R2 1 -$R1
Goto +2
  StrCpy $R2 $R1
Delete $R0
StrCpy $R0 $R2
Pop $R2
Pop $R1
Exch $R0
FunctionEnd

The Function v2

Function GenRanNum
Push $R0
GetTempFileName $R0
Delete $R0
StrCpy $R0 $R0 1 -5
IntFmt $R0 "%u" 0x$R0
StrCpy $R0 $R0 "" -1
Exch $R0
FunctionEnd

Example

Afrow UK used this to generate random BrandingImages for placing on the side of my installer during the installation process.

You can use the value generated like so:

  IntCmp $R2 2 0 0 +3
    SetBrandingImage "$TEMP\ddayupdates\left_logo1.bmp"
 
Goto set_bitmap_end
  IntCmp $R2 5 0 0 +3
    SetBrandingImage "$TEMP\ddayupdates\left_logo2.bmp"
 
Goto set_bitmap_end
  IntCmp $R2 7 0 0 +3
    SetBrandingImage "$TEMP\ddayupdates\left_logo3.bmp"
 
Goto set_bitmap_end
    SetBrandingImage "$TEMP\ddayupdates\left_logo4.bmp"
 
set_bitmap_end:

-Stu