Handy random number generator (not cryptographically strong, just trivial)

Share actions you've created here.
Post Reply
Jesse
Posts: 11
Joined: Wed Jul 27, 2011 1:52 pm

Handy random number generator (not cryptographically strong, just trivial)

Post by Jesse » Tue May 29, 2018 4:09 pm

Sometimes my roommate and I are coding up something in Secondlife, and we need a large random number to hardcode a channel not likely to be used by anybody else. It would be handy to just have a 3Dclip action to choose such a number for us, so I made one. :)

Code: Select all

Dim max,min
If(IsNumeric(clipboard.value)) Then
max=CLng(clipboard.value)
Else
  max=1e9
End If
Randomize
clipboard.value = CStr(Int(max*Rnd))
Use: first, prepare an integer in the clipboard you want to act as a "Max" such that 0 <= random integer < Max

For example, if you choose 10 you may get anything 0..9. I am a programmer, so counting from zero and not counting the cardinal is just how I roll. But if you want to count from 1 and include the cardinal (EG 1..10), just tack a +1 onto the end of the final line of code.

Then apply this action to that "max" value and the fresh random integer value will be put into your clipboard.

As a bonus, if you choose a "max" that is not a number then this will just choose a max of one billion instead. That's a Fermi approximation of the the range we're normally working with in SL as channel is a signed 32 bit number, so it's convenient for us to not always have to specify the max value. If you'd like a different default max, just alter line 5.

Happy entropy folks! :9

Jesse
Posts: 11
Joined: Wed Jul 27, 2011 1:52 pm

Re: Handy random number generator (not cryptographically strong, just trivial)

Post by Jesse » Thu Jul 11, 2019 11:17 pm

Huh. I updated this script, and must have forgotten to get the latest version over here.

Code: Select all

Dim max,result
max=InputBox("Max Value?", "Random Number Generator", "1e9")
If(max <> "") Then 'Detects cancel click, silently exits
  If((not IsNull(max)) and IsNumeric(max)) Then
    If(max>=1) Then
      Randomize
      result = CStr(Int(CLng(max)*Rnd))
      MsgBox("Random Number selected, and placed in your clipboard! :D"& vbCrLf & vbCrLf & CStr(result))
      clipboard.value = result
    Else
      MsgBox("Sorry, we need max to be one or greater, not "& CStr(max) &".")
    End If
  Else
    MsgBox("Sorry.. """& CStr(max) &""" isn't really a valid max value.")
  End If
End If

For this version of the script, you can assign it to a shortcut key or else just apply the action to any random element in your clipboard stack.

The element that you select is irrelevant to the operation.

You will be presented with a prompt for the maximum number you want to use, defaulting to 1e9 (one billion in fancy talk).
The resulting random value will be 0 <= value < max
So if you entered 10 as a random value, you'll get 0 through 9 as an answer.

The answer will be announced in a text box, and also added to the clipboard stack. (it will not replace the value that you've selected).

Post Reply