Closed TehAwesomestKitteh closed 8 months ago
I required this for my game and figured I should share it with someone who cares about the Big class!
This gets a random number between min
and max
.
static func get_random_point(min: Big, max: Big) -> Big:
if min.isEqualTo(max):
return max
var random_mantissa = randf_range(min.mantissa, max.mantissa)
var random_exponent = randi_range(min.exponent, max.exponent)
return Big.new(random_mantissa, random_exponent)
Not related: maybe you should convert the method names to snake_case to fit in with Godot's style guide. But not important!
I required this for my game and figured I should share it with someone who cares about the Big class!
This gets a random number between
min
andmax
.static func get_random_point(min: Big, max: Big) -> Big: if min.isEqualTo(max): return max var random_mantissa = randf_range(min.mantissa, max.mantissa) var random_exponent = randi_range(min.exponent, max.exponent) return Big.new(random_mantissa, random_exponent)
Not related: maybe you should convert the method names to snake_case to fit in with Godot's style guide. But not important!
Cool addition, but I think that function can be improved. There will be no mantissa range if min and max have the same mantissa.
Wait, hang on! Your reply made me realize a problem: If you do get_random_point(Big.new(4), Big.new(40)), it can only be 4 or 40, because mantissa will not change but exponent can. I'll try to work on a solution
Oh, hell, I currently can't get it to work. This is nightmarish. And I just know the solution will be so simple and easy that it'll make me mad. Good lord
static func get_random_point(min: Big, max: Big) -> Big:
# Ensure min is less than or equal to max
if min.greater(max):
var temp = min
min = max
max = temp
if min.exponent == max.exponent:
return Big.new(randf_range(min.mantissa, max.mantissa), max.exponent)
var new_exponent := randi_range(min.exponent, max.exponent)
var new_mantissa: float
if new_exponent == min.exponent:
new_mantissa = randf_range(min.mantissa, 10.0)
elif new_exponent == max.exponent:
new_mantissa = randf_range(0, max.mantissa)
else:
new_mantissa = randf_range(1, 10)
return Big.new(new_mantissa, new_exponent)
This is my latest version. When testing from 1 to 100, 36% of the numbers are less than 10.
I give up. This works for numbers less than e308:
static func get_random_point(min: Big, max: Big) -> Big:
# Ensure min is less than or equal to max
if min.greater(max):
var temp = min
min = max
max = temp
if min.exponent == max.exponent:
return Big.new(randf_range(min.mantissa, max.mantissa), max.exponent)
if min.less(1.79769e308) and max.less(1.79769e308):
return Big.new(randf_range(min.toFloat(), max.toFloat()))
else:
print("Cry")
return Big.new(1)
Summary of changes:
Big.gd
andBig.gd4
intoGodot 3.x/Big.gd
andGodot 4.1.x/Big.gd
(since.gd
is the official script extension, not.gd4
)4.1.x
class:+
,-
,*
, and/
)other
is nowoptions
, and various options has had their defaults adjustedtoAA
and thesuffixes_aa
have also been marked@deprecated
, pending a re-write so that it's faster