src/rng

Source   Edit  

Consts

asciiPattern = {48..57, 65..90, 97..122}
This set contains letters, uppercase letters and numbers only Source   Edit  

Procs

proc fromUuidv7(uuid: string): Time {....raises: [ValueError], tags: [],
                                      forbids: [].}

Returns Time parsed out of UUID object

Warning: Only pass UUIDs from trusted sources, or be ready to catch an exception.

Source   Edit  
proc rand(dig: int): int {....raises: [OSError, ValueError], tags: [], forbids: [].}
A function that generates an integer A replacement function for rand() that uses the system CSPRNG. The final integer will always be between 1 to X (where X is the argument to the number)

Example:

echo "The dice lands on... ", $rand(6), "!"

# This is true since rand(5) returns a number
# between 1 and 5.
assert rand(5) < 6; 
Source   Edit  
proc randch(letters: openArray[char] = defaultAsciiTable): char {.
    ...deprecated: "use randomChar()", raises: [OSError, ValueError], tags: [],
    forbids: [].}
Deprecated: use randomChar()
Returns a random character Source   Edit  
proc randint(limit: int): int {....raises: [OSError, ValueError], tags: [],
                                forbids: [].}
A function that generates an integer, limit is how many digits should be in the final string. ie. 10 generates a string that is 10 digits long.

Example:

# The number is 1 digit long, so its lower than 10.
assert randint(1) < 10 
Source   Edit  
proc randomChar(pattern = asciiPattern): char {....raises: [OSError], tags: [],
    forbids: [].}
Returns a random character.

Example:

echo "Your magical character is: ", randomChar()

# Here's how to customize which character will be
# generated.
echo(
  "Your magical (numerical) character is: ",
  randomChar(pattern = {48..57})
)
Source   Edit  
proc randomString(length: int = 12; pattern = asciiPattern): string {.
    ...raises: [OSError], tags: [], forbids: [].}
Generates a random string using the system CSPRNG, the string generated by default contains basic ASCII symbols (lowercase, uppercase and numbers only), which should be safe to use for all sorts of purposes. You can change which characters are generated by switching the pattern parameter.

Example:

echo "Your magical string is: ", randomString()

# You can customize the length of the generated string.
echo "Your magical short string is: ", randomString(4)

# You can customize the character generated with the 
# pattern parameter.
echo(
  "Your magical (numerical) string is: ",
  randomString(pattern = {48..57})
)
Source   Edit  
proc randstr(length: int = 16; table: openArray[char] = defaultAsciiTable): string {.
    ...deprecated: "use randomString()", raises: [OSError, ValueError], tags: [],
    forbids: [].}
Deprecated: use randomString()
Returns a random string, A function to generate a safe random string.

Example:

echo "Your magical string is the following: ", randstr()
assert randstr(length = 20).len() == 20
Source   Edit  
proc sample(a: string): char {....raises: [OSError, ValueError], tags: [],
                               forbids: [].}
Returns a random character from a string

Example:

let magicalString = "Magic!"
echo "Your magical character from the magical string is ", sample(magicalString)
assert sample(magicalString) in magicalString
Source   Edit  
proc sample[T](a: openArray[T]): T
Returns a random element from an array.

Example:

let array = @["Jack", "Kate", "Adam", "Julie"]
echo "Your magic name is ", sample(array), "!"
assert sample(array) in array
Source   Edit  
proc uuidv4(): string {....raises: [OSError], tags: [], forbids: [].}

Generates a UUID (version 4). This type of UUID is completely random.

This implementation is based on nim-uuid4 by Matt Cooper (@vtbassmatt on GitHub)

Example:

echo uuidv4()

# Statistically unlikely to fail
assert uuidv4() != uuidv4()
Source   Edit  
proc uuidv7(time = getTime()): string {....raises: [OSError], tags: [], forbids: [].}

Generates an UUID (version 7) This type of UUID is generated from a timestamp.

Using UUID v7 is recommended only when leaking ID creation time is acceptable, and you need to avoid index fragmentation in a database. Use UUID v4 if you need to avoid leaking ID creation time.

Source   Edit