How do I make a randomizer only give out whole numbers

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

I need to randomize the location of the enemies in my game and I have it set so it would depend on a random number, but Godot keeps giving decimals which don’t work with my code. Heres a part of my code:

`if my_random_number ==1:
	$RandoSpriteSheet.position.x = 577
	$RandoSpriteSheet.position.y = 300
	$Friend.position.x = -305
	$Friend.position.y = -14`

I need to make it so it is only whole number values being outputted.

Thanks

:bust_in_silhouette: Reply From: jgodfrey

Here’s a set of random number utility functions I tend to keep handy…

onready var rng = RandomNumberGenerator.new()

func _ready():
	rng.randomize()

func get_random_int_between(min_val, max_val):
	return rng.randi_range(min_val, max_val)

func get_random_float_between(min_val, max_val):
	return rng.randf_range(min_val, max_val)

func get_random_bool():
	return rng.randi() % 2 == 1

# Return 1 or -1
func get_random_direction():
	return (randi() & 2) - 1

So, specifically, you could call the above get_random_int_between() for the case you mention. Or, just use its internals as necessary…

:bust_in_silhouette: Reply From: CassanovaWong

you can convert floats with int() and/or convert negatives with abs() or do both with abs(int())