Snippets
  • Uploaded By: Raccoon
  • Added: 11 months ago
  • Updated: 11 months ago
  • mIRC Version: 6.30
  • Hits: 478
  • Size: 2.71KB
  • Downloads: 6
  • Review By: maroon



$RandNow() -- random numbers based on clock time. 1.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.

    
  2    0  Login to Vote.


Source Code:
  1. ; RandNow is a random number generator that produces a whole number that changes
  2. ; periodically with time. Ie, each minute or hour or daily or every 37 seconds.
  3. ; This identifier will return the same random integer until the period elapses.
  4. ; The period is aligned against UNIX clock time; number of seconds since epoch.
  5. ; Usage: $RandNow(Rnd_From, Rnd_To, Clock_Period, Seed_String)
  6. ; Example: $RandNow(-100, 100, 3600, Jennie Garth) <-- Produces a value between
  7. ; -100 and 100 that changes by the hour. "Jennie Garth" is an arbitrary string.
  8. ; //say The price of scripts this hour is $RandNow(15,60,3600,scriptcost) gold.
  9.  
  10. ; $RandNow(from, to, period, seed)
  11. RandNow {
  12. var %from = $1, %to = $2, %range = $calc(%to - %from + 1)
  13. return $calc($base($right($sha1($int($calc($ctime / $3)) $4),13),16,10) % %range + %from)
  14. } ; by Raccoon 2018 -- https://mircscripts.net/j6hi0
  15.  
  16.  
  17. ; RandNow2 is like RandNow, but introduces a distribution curve. Instead of
  18. ; producing numbers that each have equal chances of being chosen at random,
  19. ; you can define affinity and rarity for certain numbers within the range.
  20. ; Generally speaking, numbers chosen within a curve are more common nearer the
  21. ; center of the range, and numbers nearer the edges are less common, or rare.
  22. ; You can also shift the curve so numbers are more common at one of the edges.
  23. ; To grasp this concept, it's best to play around with these nested rands:
  24. ; $r($r(1,100),$r(1,100)) -- common bell curve, center bulge around 50.
  25. ; $r($r(1,50),$r(50,100)) -- triangular distribution, peaking at center 50.
  26. ; $r($r(1,1),$r(1,100)) -- slope distribution, 1 = frequent, 100 = rare.
  27. ; $r($r(1,20),$r(80,100)) -- flattened center, tapered at each of the ends.
  28. ; $r($r(1,100),$r(80,100)) -- sloping up 1-80 and leaning heavily over 80-100.
  29. ; If you were to collect 10,000 samples of each nested function above, you
  30. ; can plot these curves as a graph in your favorite spreadsheet software.
  31. ; Usage: $RandNow2(From1, To1, From2 ,To2, Clock_Period, Seed_String)
  32. ; Example: $RandNow2(1,100, 1,100, 3600, Jennie Garth)
  33.  
  34. ; $RandNow2(from1, to1, from2, to2, period, seed)
  35. RandNow2 {
  36. var %from1 = $1, %to1 = $2, %range1 = $calc(%to1 - %from1 + 1)
  37. var %from2 = $3, %to2 = $4, %range2 = $calc(%to2 - %from2 + 1)
  38. var %period = $5, %seed = $6, %hash = $sha1($int($calc($ctime / %period)) %seed)
  39. var %from3 = $calc($base($left(%hash,13),16,10) % %range1 + %from1)
  40. var %to3 = $calc($base($mid(%hash,14,13),16,10) % %range2 + %from2)
  41. if (%from3 > %to3) var %from3 = %to3, %to3 = $v1
  42. var %range3 = $calc(%to3 - %from3 + 1)
  43. return $calc($base($mid(%hash,27,13),16,10) % %range3 + %from3)
  44. } ; by Raccoon 2018 -- https://mircscripts.net/j6hi0


Comments
No Comments.

Login to Comment.