Skip to content
⌘ NSIS Forum Archive

Random Pick

24 posts

Yathosho#

Random Pick

i'm having an .ini file that looks like this

[Section]
n0=value 1
n1=value 2
n2=value 3
etc.

i have an nsis script that counts the number of n's in this .ini. is there a clever function to pick a random n?
DOCa Cola#
here
Are you a developer who uses NSIS to distribute your application? Are you a Winamp plug-in developer who wants to use NSIS to distribute your plug-in? Have suggestions for other people like you? This is the place.

i am using that way to get a random number too, but it only brings one 'random' number per application run

DOCa Cola
Afrow UK#
So,

StrCpy $0 $HWNDPARENT 1 -1

Edit: It's not very good, and always outputs the same digit if you use it lots of times in the same installer.
I will find another way for generating random numbers.

-Stu
Afrow UK#
The best thing I could use was GetFileTime of GetTempFileName

Usage:
Call MakeRandomNum
Pop $R0

$R0 = random number

Function MakeRandomNum
Push $R0
Push $R1
Push $R2
GetTempFileName $R0
GetFileTime $R0 $R1 $R2
SetDetailsPrint none
Delete $R0
SetDetailsPrint both
StrCpy $R0 $R2 1 -1
Sleep 500
Pop $R2
Pop $R1
Exch $R0
FunctionEnd
Just put some Sleep 1000 between calls if you want to get random numbers many times within one installer.

-Stu
MoNKi#
Try this:


OutFile "Random.exe"
Name "Random"

; Usage:
; Push Seed (or previously generated number)
; Call RandomNumber
; Pop Generated Random Number
Function RandomNumber
Exch $R0

IntOp $R0 $R0 * "13"
IntOp $R0 $R0 + "3"
IntOp $R0 $R0 % "1048576" ; Values goes from 0 to 1048576 (2^20)

Exch $R0
FunctionEnd

Section ""
Push $HWNDPARENT ;Seed
Loop:
Call RandomNumber
Pop $R0
MessageBox MB_YESNO $R0 IDNO +3
Push $R0 ;The new seed
Goto Loop
SectionEnd
Joel#
That works too, well with a little changes... 👍

OutFile "Random.exe"
Name "Random"

; Usage:
; Push Seed (or previously generated number)
; Call RandomNumber
; Pop Generated Random Number
Function RandomNumber
Exch $R0

IntOp $R0 $R0 * "13"
IntOp $R0 $R0 + "3"
IntOp $R0 $R0 % "1048576" ; Values goes from 0 to 1048576 (2^20)

Exch $R0
FunctionEnd

Function .onInit
Push $HWNDPARENT ;Seed
Loop:
Call RandomNumber
Pop $R0
MessageBox MB_YESNO "Random number: $R0 $\r$\nCreate another one?" IDNO +3
Push $R0 ;The new seed
Goto Loop
Abort
FunctionEnd

Section ""
;I'm a ghost, boo
SectionEnd
BTW: monki has post... 😁 😉
Good script, dude
🙂
Yathosho#
lobo's script always picks the same numbers in the same order.

monki's script generates random numbers, but they repeat until one restarts the application.
Yathosho#
maybe we can get a real random function as plugin by someone? :P

i figured, the two examples above work worse, the lower the maximum is.

[edit]i simply raised the maximum by 1 and hope the function will never ever be the max (which it hasnt been yet).
DOCa Cola#
maybe someone can convert a number generator from the Blitz++ source to a nsis plugin? 🙂
just an idea


DOCa Cola
MoNKi#
A new code, now you can set a max value for the random number, is only a test and I know that needs a cleanup. Only has a problem, if you set the Max value to 100 (or 10,1000,10000,...), for example, the number 100 has the same probability that the group (0-99):

Probability of 100 = 1/2
Probability of numbers from 0 to 99 = (1/2)/100

But if you set the max at 99 (9,999,9999,9999,...) all the numbers has the same probability.
MoNKi#
Last version, I think all works ok. Generates a number between 0 and Max
lzandman#
I've just created a NSIS plugin called nsRandom, based on Borland Delphi 7's Random() function, that can do the trick. It can return random values in a specified range or return values between 0 and 1.

Download it here. An example NSI-script is included. Please comment on it.
Joost Verburg#
Is it not possible to make the plug-in a little smaller? Using C something like this will have a size of about 3 KB.
lzandman#
Originally posted by Joost Verburg
Is it not possible to make the plug-in a little smaller? Using C something like this will have a size of about 3 KB.
Yeah, I know. I'll try to get it done in VC++, but I currently don't have Visual Studio installed. I'll keep it in mind though 🙂
brainsucker#
System::Call 'kernel32::GetTickCount() i.r0'

will place to r0 a number of ms since OS uptime (DWORD value).

if ms precision is not enough for you, use the following: (that value will never repeat, but it is int64, so use Int64Op to work with it).

System::Call 'kernel32::QueryPerformanceCounter(*l.r0)'

anyway both solution will add ~6 kb compressed 🙂 sorry 🙂
DOCa Cola#
Originally posted by Joost Verburg
Is it not possible to make the plug-in a little smaller? Using C something like this will have a size of about 3 KB.
beside that it works really nice 🙂
thx!

DOCa Cola
Afrow UK#
There is a script on the NSIS archive that generates a random number from 0-9.
It is based upon GetFileTime, so is not officially random.



-Stu
lzandman#
Originally posted by DOCa Cola
beside that it works really nice 🙂
thx!

DOCa Cola
You're welcome 🙂

The NSIS developers want every part of NSIS to be as small as possible. Unfortunately that doesn't work when you are programming in Delphi. Delphi generated exe/dll's are usually bigger than those created using VC++. But I prefer Delphi over VC++.

Personally I don't care about this size thing, because my installers are always several megabytes big, so a plugin of 150 kB isn't that much of a problem. 😁

But Joost is right that the nsRandom plugin is quite big considering it's rather limited functionality.
Afrow UK#
Indeed, 150kb's is nothing nowadays (well, for non-56k users anyway!)

Thanks so much for plugin support!

-Stu 🙂
brainsucker#
Can't leave happily without pushing system idea 😉
Push Max, call random, pop value. example:

Push 100
call Random
Pop $0
MessageBox MB_OK "Random $0"

Function Random
Exch $0
Push $1
System::Call 'kernel32::QueryPerformanceCounter(*l.r1)'
System::Int64Op $1 % $0
Pop $0
Pop $1
Exch $0
FunctionEnd