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