$RandNow() - periodic random number generator

By raccoon on Aug 05, 2018

$RandNow() lets you generate a random number that remains attached to a period of clock time.

For instance, you can have it produce a new value on each hour (on the top of the hour), or every quarter hour or 30 seconds or what-have-you. Rather than generating a new value with a /timer command, and storing that value to a variable, you can always retrieve the same random value with this function until the clock time has changed and the period has elapsed, generating a new value instead.

To achieve this, $RandNow() references $ctime. It creates an $sha1() hash using some subset value of $ctime (period) and an arbitrary (seed) string. The seed string can be kept secret, like a password, if using this function to generate random values being used in a game so that the next random values cannot be predicted in advance by players.

This function was written for a role play game where buy & sell prices for game items would vary each hour.

Requires mIRC v6.30 or above.


; RandNow is a random number generator that produces a whole number that changes
;  periodically with time. Ie, each minute or hour or daily or every 37 seconds.
; This identifier will return the same random integer until the period elapses.
; The period is aligned against UNIX clock time; number of seconds since epoch.
; Usage:    $RandNow(Rnd_From, Rnd_To, Clock_Period, Seed_String)
; Example:  $RandNow(-100, 100, 3600, Jennie Garth) <-- Produces a value between
; -100 and 100 that changes by the hour.  "Jennie Garth" is an arbitrary string.
; //say The price of scripts this hour is $RandNow(15,60,3600,scriptcost) gold.

; $RandNow(from, to, period, seed)
RandNow {
  var %from = $1, %to = $2, %range = $calc(%to - %from + 1)
  return $calc($base($right($sha1($int($calc($ctime / $3)) $4),13),16,10) % %range + %from)
} ; by Raccoon 2018 -- http://hawkee.com/snippet/19731/


; RandNow2 is like RandNow, but introduces a distribution curve.  Instead of
;  producing numbers that each have equal chances of being chosen at random,
;  you can define affinity and rarity for certain numbers within the range.
; Generally speaking, numbers chosen within a curve are more common nearer the
;  center of the range, and numbers nearer the edges are less common, or rare.
; You can also shift the curve so numbers are more common at one of the edges.
; To grasp this concept, it's best to play around with these nested rands:
;   $r($r(1,100),$r(1,100))  -- common bell curve, center bulge around 50.
;   $r($r(1,50),$r(50,100))  -- triangular distribution, peaking at center 50.
;   $r($r(1,1),$r(1,100))    -- slope distribution, 1 = frequent, 100 = rare.
;   $r($r(1,20),$r(80,100))  -- flattened center, tapered at each of the ends.
;   $r($r(1,100),$r(80,100)) -- sloping up 1-80 and leaning heavily over 80-100.
; If you were to collect 10,000 samples of each nested function above, you
;  can plot these curves as a graph in your favorite spreadsheet software.
; Usage:    $RandNow2(From1, To1, From2 ,To2, Clock_Period, Seed_String)
; Example:  $RandNow2(1,100, 1,100, 3600, Jennie Garth)

; $RandNow2(from1, to1, from2, to2, period, seed)
RandNow2 {
  var %from1 = $1, %to1 = $2, %range1 = $calc(%to1 - %from1 + 1)
  var %from2 = $3, %to2 = $4, %range2 = $calc(%to2 - %from2 + 1)
  var %period = $5, %seed = $6, %hash = $sha1($int($calc($ctime / %period)) %seed)
  var %from3 = $calc($base($left(%hash,13),16,10) % %range1 + %from1)
  var %to3 = $calc($base($mid(%hash,14,13),16,10) % %range2 + %from2)
  if (%from3 > %to3) var %from3 = %to3, %to3 = $v1
  var %range3 = $calc(%to3 - %from3 + 1)
  return $calc($base($mid(%hash,27,13),16,10) % %range3 + %from3)
} ; by Raccoon 2018 -- http://hawkee.com/snippet/19731/


Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.