RngUtils

An asset by shank
The page banner background of a mountain and forest
RngUtils hero image

Quick Information

0 ratings
RngUtils icon image
shank
RngUtils

Random number generation utils for Godot 4- Random integer range- Random float range- Random item(s) from an array- Random item(s) from an array with weighted

Supported Engine Version
4.0
Version String
1.1
License Version
MIT
Support Level
community
Modified Date
11 months ago
Git URL
Issue URL

Godot RngUtils

Random number generation utils for the Godot 4

Features

  • Random integer range
  • Random float range
  • Random item(s) from an array
  • Random item(s) from an array with weighted

Installation

  1. Clone or download a copy of this repository.
  2. Enable the addon in the project settings.
  3. Restart Godot IDE.

Usage

Random integer range

int_range(from: int, to: int, rng: RandomNumberGenerator = null) -> int

Returns a pseudo-random integer between from and to (inclusive)

RngUtils.int_range(1, 100)
# output: 89

Random float range

float_range(from: float, to: float, rng: RandomNumberGenerator = null)

Returns a pseudo-random float between from and to (inclusive)

RngUtils.float_range(0.1, 5.1)
# output: 3.89661073684692

Random item(s) from an array

array(array: Array, num: int = 1, unique: bool = false, rng: RandomNumberGenerator = null) -> Array

Returns one or multiple random items from an array

var array := ["a", "b", "c", "d"]
# random one item
RngUtils.array(array)
# output: ["a"]

# random four items(not unique)
RngUtils.array(array, 4)
# output: ["d", "d", "d", "c"]

# random four items(unique)
RngUtils.array(array, 4, true)
# output: ["c", "b", "a", "d"]

Random item(s) from an array with weighted

array_with_weighted(weights: Array[Dictionary], num: int = 1, unique: bool = false, rng: RandomNumberGenerator = null) -> Array[Dictionary]

  • Dictionary key weight require
  • Returns one or multiple random items from an array with weighted
var weights: Array[Dictionary] = [
    {"value":"a","txt": "apple", "weight": 5.0},
    {"value":"b", "weight": 10.0},
    {"value":"c", "weight": 20.0},
    {"value":"d", "weight": 40.0},
    {"value":"e", "weight": 25.0},	
]

# random one item
RngUtils.array_with_weighted(weights, 1)
# output: [{ "value": "c", "weight": 20 }]

# random four items(not unique)
RngUtils.array_with_weighted(weights, 4)
# output: [{ "value": "c", "weight": 20 }, { "value": "d", "weight": 40 }, { "value": "e", "weight": 25 }, { "value": "d", "weight": 40 }]

# random four items(unique)
RngUtils.array_with_weighted(weights, 4, true)
# output: { "value": "d", "weight": 40 }, { "value": "c", "weight": 20 }, { "value": "a", "txt": "apple", "weight": 5 }, { "value": "b", "weight": 10 }]

Random number generation utils for Godot 4

- Random integer range
- Random float range
- Random item(s) from an array
- Random item(s) from an array with weighted

Reviews

0 ratings

Your Rating

Headline must be at least 3 characters but not more than 50
Review must be at least 5 characters but not more than 500
Please sign in to add a review

Quick Information

0 ratings
RngUtils icon image
shank
RngUtils

Random number generation utils for Godot 4- Random integer range- Random float range- Random item(s) from an array- Random item(s) from an array with weighted

Supported Engine Version
4.0
Version String
1.1
License Version
MIT
Support Level
community
Modified Date
11 months ago
Git URL
Issue URL

Open Source

Released under the AGPLv3 license

Plug and Play

Browse assets directly from Godot

Community Driven

Created by developers for developers