Problem whit randomness

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RubRub

I have a problem with this system, in which at the beginning it does generate a random number, but when restarting the scene it generates the same numbers again in the same order, And for what I’m doing it needs to be completely random.

This is the base code

var rng = RandomNumberGenerator.new()

var number = 0

func _ready():
	number = stepify(rng.randi_range(1,100) 1)

Help pls.

:bust_in_silhouette: Reply From: jgodfrey

You need to add a single call to randomize() prior to using any of the random functions. So, this…

func _ready():
    randomize()
    number = stepify(rng.randi_range(1,100) 1)

But is there a way to make one number more likely than another? , such as 1 = 10% chance or 100 = %1

RubRub | 2022-02-08 05:01

Thank you Bro

RubRub | 2022-02-08 05:21

I don’t think there is build in solution for chance. But you can probably code it yourself with some for or while loops.

sporx13 | 2022-02-08 10:43

Do you have a finite list of numbers each with a given probability value or do you want to sample from a probability distribution function?

skysphr | 2022-02-08 18:16

I want a sample of a probability distribution function

RubRub | 2022-02-09 05:30

Look into rejection sampling. Here’s an excerpt from a simple implementation I wrote a while ago:

func edges_density(x: float) -> float:
    return pow(2.0 * x - 1, 6)

func rejection_sample(p_func: FuncRef) -> float:
    while true:
        var u: float = randf()
        var y: float = randf()
        if y < p_func.call_func(u):
            return u

(...)
var sample = rejection_sample(funcref(self, "edges_density"))
# Sample is between 0 and 1

skysphr | 2022-02-09 12:11