$rands » History » Version 3
Per Amundsen, 07/07/2023 11:13 PM
1 | 1 | Per Amundsen | _Added in 3.3_ |
---|---|---|---|
2 | |||
3 | *$rands(v1,v2)* |
||
4 | |||
5 | Returns a random number or letter between v1 and v2 using encryption and high entropy. |
||
6 | |||
7 | _See also [[$rand]], [[$r]]._ |
||
8 | |||
9 | *Parameters* |
||
10 | |||
11 | 2 | Per Amundsen | table(ktable). |
12 | |*Parameter*|*Description*| |
||
13 | 3 | Per Amundsen | | v1 | Start number or letter [0-9A-Za-z]. | |
14 | | v2 | End number or letter [0-9A-Za-z]. | |
||
15 | |||
16 | 1 | Per Amundsen | if v1 and v2 are both numeric, returns random number in that range |
17 | otherwise returns a character in the range of characters v1 and v2 |
||
18 | |||
19 | valid number range -2^63 through 2^63-1 |
||
20 | |||
21 | *Example* |
||
22 | |||
23 | <pre> |
||
24 | ; Get a random number between 10 and 100. |
||
25 | //echo -ag $rands(10,100) |
||
26 | |||
27 | ; Get a random number between -10 and +10. |
||
28 | //echo -ag $rands(10,-10) |
||
29 | |||
30 | ; Get a random letter between g and y. |
||
31 | //echo -ag $rands(g,y) |
||
32 | |||
33 | ; 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 [\]^_` |
||
34 | //echo -ag $rands(A,z) |
||
35 | |||
36 | To obtain only the 52 case-insensitive characters: //echo -a $iif($rands(0,1),$rands(A,Z),$rands(a,z)) |
||
37 | |||
38 | Numeric includes numbers at the beginning of strings. |
||
39 | //echo -a $rands(1X,4X) is the same as $rands(1,4) |
||
40 | |||
41 | Only 1st character of non-numeric string is used. |
||
42 | //echo -a $rands(AB,CD) is in the range A through C |
||
43 | |||
44 | No 'good' random number generator creates equal outcome of all numbers in the range, but they should be fairly close to the average |
||
45 | //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 |
||
46 | </pre> |