Author: Raccoon
Added: 5y
Updated: 5y
mIRC: 6.30
Hits: 1,770
Downloads: 15
Review: maroon
Size: 2.8KB
2 0
Login to vote.
$RandNow() -- random numbers based on clock time.
v1.2
$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.
Usage:
$RandNow(Rnd_From, Rnd_To, Clock_Period, Seed_String)
Example:
$RandNow(-100, 100, 3600, Jennie Garth)
This produces a value between -100 and 100 that changes by the hour. "Jennie Garth" is an arbitrary string.
Example:
//echo -a The price of scripts this hour is $RandNow(15,60,3600,scriptcost) gold.
> The price of scripts this hour is 20 gold.
For clarity: Rnd_From and Rnd_To parameters must be whole integer numbers. Rnd_From must be less-than or equal-to Rnd_To. You may pass negative values.
; 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 -- https://mircscripts.net/j6hi0 ; 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 -- https://mircscripts.net/j6hi0
v1.1 Removed undesired double-dollarsigns. Clarified parameter requirements in description.
v1.2 Added RandNow2().
Similar to the built-in $totp() except the number can be forced into a range more precise than simply limiting the number of digits from 3-9..
If the value in the next time interval needs to be kept secret from others, you should treat the 4th parameter like a password.
Something that $totp() does is use 1 of the unused digits of the $sha1 hash digest to decide which of the other hex digits it uses to calculate the random number, but that's just guarding against a theoretical unknown bias in the numbers, and I don't think it's needed here. This function uses a 52-bit value as the numerator, while $totp uses 'only' 31 bits.
Negative numbers are fine, but be sure to not use fractions, as that can allow numbers to be returned outside the min/max range.