53 lines
1.5 KiB
GDScript
53 lines
1.5 KiB
GDScript
extends Node
|
|
|
|
class_name RandomClass
|
|
|
|
var _rng: RandomNumberGenerator = RandomNumberGenerator.new()
|
|
|
|
func _ready():
|
|
randomize()
|
|
|
|
# Set a specific seed for reproducible results
|
|
func set_seed(seed_value: int) -> void:
|
|
_rng.seed = seed_value
|
|
|
|
# Randomize the seed (for non-reproducible results)
|
|
func randomize() -> void:
|
|
_rng.randomize()
|
|
|
|
# Get the current seed value
|
|
func get_seed() -> int:
|
|
return _rng.seed
|
|
|
|
# Get a random integer between min and max (inclusive)
|
|
func randi_range(min_value: int, max_value: int) -> int:
|
|
return _rng.randi_range(min_value, max_value)
|
|
|
|
# Get a random float between 0.0 and 1.0
|
|
func randf() -> float:
|
|
return _rng.randf()
|
|
|
|
func randi() -> int:
|
|
return _rng.randi()
|
|
|
|
# Get a random float between min and max
|
|
func randf_range(min_value: float, max_value: float) -> float:
|
|
return _rng.randf_range(min_value, max_value)
|
|
|
|
# Get a random normalized vector
|
|
func random_unit_vector() -> Vector2:
|
|
return Vector2(randf_range(-1.0, 1.0), randf_range(-1.0, 1.0)).normalized()
|
|
|
|
# Get a random point inside a circle with radius 1
|
|
func random_point_in_circle() -> Vector2:
|
|
var r = sqrt(randf())
|
|
var theta = randf() * 2.0 * PI
|
|
return Vector2(r * cos(theta), r * sin(theta))
|
|
|
|
# Get a random point inside a circle with specified radius
|
|
func random_point_in_circle_with_radius(radius: float) -> Vector2:
|
|
return random_point_in_circle() * radius
|
|
|
|
# Get a random boolean value with specified probability
|
|
func random_bool(probability: float = 0.5) -> bool:
|
|
return randf() < probability
|