Project

General

Profile

$rands » History » Revision 2

Revision 1 (Per Amundsen, 01/31/2019 05:46 AM) → Revision 2/3 (Per Amundsen, 02/23/2023 07:19 PM)

_Added in 3.3_ 

 *$rands(v1,v2)* 

 Returns a random number or letter between v1 and v2 using encryption and high entropy. 

 _See also [[$rand]], [[$r]]._ 

 *Parameters* 

 table(ktable). 
 |*Parameter*|*Description*| 
 if v1 and v2 are both numeric, returns random number in that range 
 otherwise returns a character in the range of characters v1 and v2 

 valid number range -2^63 through 2^63-1 

 *Example* 

 <pre> 
 ; Get a random number between 10 and 100. 
 //echo -ag $rands(10,100) 

 ; Get a random number between -10 and +10. 
 //echo -ag $rands(10,-10) 

 ; Get a random letter between g and y. 
 //echo -ag $rands(g,y) 

 ; Get a random character between A and z. Since 'A' is codepoint 65 and 'z' is codepoint 122, this includes non-alphanumeric codepoints 91-96 [\]^_` 
 //echo -ag $rands(A,z) 

 To obtain only the 52 case-insensitive characters: //echo -a $iif($rands(0,1),$rands(A,Z),$rands(a,z)) 

 Numeric includes numbers at the beginning of strings. 
 //echo -a $rands(1X,4X) is the same as $rands(1,4) 

 Only 1st character of non-numeric string is used. 
 //echo -a $rands(AB,CD) is in the range A through C 

 No 'good' random number generator creates equal outcome of all numbers in the range, but they should be fairly close to the average 
 //hfree -w test | hmake test | var %i 100000 | while (%i) { hinc test $rands(1,100)) | dec %i } | var %i 1 , %a | while (%i isnum 1-100) { var %a %a %i $+ = $+ $hget(test,%i) | inc %i } | echo -a %a 
 </pre>