Skip to main content
  1. Lumesh Document/
  2. Lumesh Libs/Modules/

Lumesh Rand Module

530 words·3 mins
Table of Contents

Module Name: rand
You can view help using help rand.

Function Description: Provides random number generation, random sampling, and randomization operations.


Function Directory
#

Function CategoryFunction NameCalling FormatCore Functionality
Boolean GenerationratioRand.ratio [numerator] [denominator]Generate a boolean value based on probability
String GenerationalphaRand.alpha [length]Generate a random alphabetic string
String GenerationalphanumRand.alphanum [length]Generate a random alphanumeric string
Numeric GenerationintRand.int [min] [max]Generate a random integer
Sampling OperationchooseRand.choose [list]Randomly select an element from a list
List OperationshuffleRand.shuffle [list]Randomly shuffle the order of a list

Function Details
#

1. Rand.ratio - Probability Boolean Generation
#

Parameter Description:

Parameter ModeParameter TypeDescription
No Parameters-Defaults to returning true with 50% probability
Single ParameterFloatSpecifies the probability of true (0.0 to 1.0)
Two ParametersInteger, IntegerSpecifies probability in fraction form (e.g., 3 5 means 3/5 probability)

Return Value: Boolean
Example:

1# Basic call
2Rand.ratio          # => true (50% probability)
3Rand.ratio 0.3      # => false (70% probability)
4
5# Fraction form
6Rand.ratio 1 4      # => true (25% probability)

2. Rand.alpha - Random Alphabetic String
#

Parameter Description:

Parameter ModeParameter TypeDescription
No Parameters-Returns a single random letter
Single ParameterIntegerSpecifies the length of the generated string

Return Value: String
Example:

1Rand.alpha       # => "k"
2Rand.alpha 5     # => "qjZRy"

3. Rand.alphanum - Random Alphanumeric String
#

Parameter Description:

Parameter ModeParameter TypeDescription
No Parameters-Returns a single random letter or digit
Single ParameterIntegerSpecifies the length of the generated string

Return Value: String
Example:

1Rand.alphanum        # => "7"
2Rand.alphanum 8      # => "F3g9K2wQ"

4. Rand.int - Random Integer Generation
#

Parameter Description:

Parameter ModeParameter TypeDescription
No Parameters-Returns a random integer in the entire i64 range
Single ParameterIntegerReturns an integer in the range [0, max] (supports negative numbers)
Two ParametersInteger, IntegerReturns an integer in the range [min, max]

Return Value: Integer
Example:

1Rand.int             # => -327683491 (example)
2Rand.int 100         # => 57
3Rand.int -50 50      # => -12

5. Rand.choose - List Random Sampling
#

Parameter Description:

ParameterTypeDescription
1ListThe data list to sample from

Return Value: Any element from the list (returns None for an empty list)
Example:

1Rand.choose [1 2 3]          # => 2
2Rand.choose ["A" "B" "C"]    # => "B"

6. Rand.shuffle - List Randomization
#

Parameter Description:

ParameterTypeDescription
1ListThe data list to randomize

Return Value: A new list with elements in random order
Example:

1ori = List.from(0..12)
2Rand.shuffle ori    # => [3, 8, 7, 11 ...]

Output Example:

1┌───┬───┬───┬────┬───┬───┬───┬───┬───┬────┬───┬───┐
2│ 3 │ 8 │ 7 │ 11 │ 9 │ 2 │ 5 │ 0 │ 6 │ 10 │ 4 │ 1 │
3└───┴───┴───┴────┴───┴───┴───┴───┴───┴────┴───┴───┘

Usage Scenario Examples
#

Batch Generate Test Data:

1# Generate 10 random usernames
2fn gen_user() {
3  name = (List.join "-" [Rand.alpha(3), Rand.int(1000, 9999)])
4  age = (Rand.int 18 60)
5  return [name, age]
6}
7
8repeat 10 { gen_user | print }

Output Example:

1+-------------------------+
2| USERNAME      AGE       |
3+=========================+
4| xQz-4823      34        |
5| kFt-7391      22        |
6| ...           ...       |
7+-------------------------+

Related