Choosing random float from two ranges

Godot Version

Godot_v4.0.2-stable_win64

Question

I need to pick a random float from two ranges (-32.0, -200.0) and (32.0, 200.0).
I basically want to avoid the range(-32.0, 32.0).
I want to loop through the random ranges to get a new random float everytime.

You can use randf_range to get a random float from one of your ranges, and then use more randomness to negate it with a 50% chance.

func _ready():
    # Initialize random generator - only do this once!
    randomize()

func random_float():
    # randomly generate a number from one of the two ranges
    var f = randf_range(32.0, 200.0)

    # randomly choose whether to negate it
    var i = randi() % 2
    if i == 0:
        f = -f

    return f
1 Like

A condensed version:

func random_float():
	return randf_range(32.0, 200.0) * (randi() % 2 * 2 - 1)
1 Like

Many thanks