Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exposed randi_range to global funcs + renamed rand_range to randf_range #40718

Merged
merged 1 commit into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions core/math/expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ const char *Expression::func_name[Expression::FUNC_MAX] = {
"randomize",
"randi",
"randf",
"rand_range",
"randf_range",
"randi_range",
"seed",
"rand_seed",
"deg2rad",
Expand Down Expand Up @@ -127,7 +128,7 @@ String Expression::get_func_name(BuiltinFunc p_func) {
int Expression::get_func_argument_count(BuiltinFunc p_func) {
switch (p_func) {
case MATH_RANDOMIZE:
case MATH_RAND:
case MATH_RANDI:
case MATH_RANDF:
return 0;
case MATH_SIN:
Expand Down Expand Up @@ -178,7 +179,8 @@ int Expression::get_func_argument_count(BuiltinFunc p_func) {
case MATH_POW:
case MATH_EASE:
case MATH_STEPIFY:
case MATH_RANDOM:
case MATH_RANDF_RANGE:
case MATH_RANDI_RANGE:
case MATH_POLAR2CARTESIAN:
case MATH_CARTESIAN2POLAR:
case LOGIC_MAX:
Expand Down Expand Up @@ -397,17 +399,22 @@ void Expression::exec_func(BuiltinFunc p_func, const Variant **p_inputs, Variant
Math::randomize();

} break;
case MATH_RAND: {
case MATH_RANDI: {
*r_return = Math::rand();
} break;
case MATH_RANDF: {
*r_return = Math::randf();
} break;
case MATH_RANDOM: {
case MATH_RANDF_RANGE: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::random((double)*p_inputs[0], (double)*p_inputs[1]);
} break;
case MATH_RANDI_RANGE: {
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
*r_return = Math::random((int)*p_inputs[0], (int)*p_inputs[1]);
} break;
case MATH_SEED: {
VALIDATE_ARG_NUM(0);
uint64_t seed = *p_inputs[0];
Expand Down
5 changes: 3 additions & 2 deletions core/math/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ class Expression : public Reference {
MATH_MOVE_TOWARD,
MATH_DECTIME,
MATH_RANDOMIZE,
MATH_RAND,
MATH_RANDI,
MATH_RANDF,
MATH_RANDOM,
MATH_RANDF_RANGE,
MATH_RANDI_RANGE,
Comment on lines +78 to +79
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're up for one last refactoring, it would be good to use the opportunity to change the order here:

MATH_RANDI
MATH_RANDF
MATH_RANDI_RANGE
MATH_RANDF_RANGE

(i.e. int first, then float for the ranged version too)

And then adapt the order in all places where these enums are used to keep the same order as the enum.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or the other way around (float, then int) if we want alphabetical order instead:

MATH_RANDF
MATH_RANDI
MATH_RANDF_RANGE
MATH_RANDI_RANGE

MATH_SEED,
MATH_RANDSEED,
MATH_DEG2RAD,
Expand Down
4 changes: 4 additions & 0 deletions core/math/math_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,7 @@ double Math::random(double from, double to) {
float Math::random(float from, float to) {
return default_rand.random(from, to);
}

int Math::random(int from, int to) {
return default_rand.random(from, to);
}
2 changes: 1 addition & 1 deletion core/math/math_funcs.h
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ class Math {

static double random(double from, double to);
static float random(float from, float to);
static real_t random(int from, int to) { return (real_t)random((real_t)from, (real_t)to); }
static int random(int from, int to);

static _ALWAYS_INLINE_ bool is_equal_approx(real_t a, real_t b) {
// Check for exact equality first, required to handle "infinity" values.
Expand Down
21 changes: 4 additions & 17 deletions core/math/random_number_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class RandomNumberGenerator : public Reference {
static void _bind_methods();

public:
_FORCE_INLINE_ void set_seed(uint64_t seed) { randbase.seed(seed); }
_FORCE_INLINE_ void set_seed(uint64_t p_seed) { randbase.seed(p_seed); }

_FORCE_INLINE_ uint64_t get_seed() { return randbase.get_seed(); }

Expand All @@ -53,24 +53,11 @@ class RandomNumberGenerator : public Reference {

_FORCE_INLINE_ real_t randf() { return randbase.randf(); }

_FORCE_INLINE_ real_t randf_range(real_t from, real_t to) { return randbase.random(from, to); }
_FORCE_INLINE_ real_t randf_range(real_t p_from, real_t p_to) { return randbase.random(p_from, p_to); }

_FORCE_INLINE_ real_t randfn(real_t mean = 0.0, real_t deviation = 1.0) { return randbase.randfn(mean, deviation); }
_FORCE_INLINE_ real_t randfn(real_t p_mean = 0.0, real_t p_deviation = 1.0) { return randbase.randfn(p_mean, p_deviation); }

_FORCE_INLINE_ int randi_range(int from, int to) {
int range;
int min;
if (to > from) {
range = to - from + 1;
min = from;
} else if (to < from) {
range = from - to + 1;
min = to;
} else { // from == to
return from;
}
return randbase.rand(range) + min;
}
_FORCE_INLINE_ int randi_range(int p_from, int p_to) { return randbase.random(p_from, p_to); }

RandomNumberGenerator() {}
};
Expand Down
15 changes: 15 additions & 0 deletions core/math/random_pcg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,18 @@ double RandomPCG::random(double p_from, double p_to) {
float RandomPCG::random(float p_from, float p_to) {
return randf() * (p_to - p_from) + p_from;
}

int RandomPCG::random(int p_from, int p_to) {
int range;
int min;
if (p_to > p_from) {
range = p_to - p_from + 1;
min = p_from;
} else if (p_to < p_from) {
range = p_from - p_to + 1;
min = p_to;
} else { // from == to
return p_from;
}
return rand(range) + min;
}
2 changes: 1 addition & 1 deletion core/math/random_pcg.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class RandomPCG {

double random(double p_from, double p_to);
float random(float p_from, float p_to);
real_t random(int p_from, int p_to) { return (real_t)random((real_t)p_from, (real_t)p_to); }
int random(int p_from, int p_to);
};

#endif // RANDOM_PCG_H
19 changes: 17 additions & 2 deletions modules/gdscript/doc_classes/@GDScript.xml
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@
[/codeblock]
</description>
</method>
<method name="rand_range">
<method name="randf_range">
<return type="float">
</return>
<argument index="0" name="from" type="float">
Expand All @@ -931,7 +931,22 @@
<description>
Random range, any floating point value between [code]from[/code] and [code]to[/code].
[codeblock]
prints(rand_range(0, 1), rand_range(0, 1)) # Prints e.g. 0.135591 0.405263
prints(randf_range(-10, 10), randf_range(-10, 10)) # Prints e.g. -3.844535 7.45315
[/codeblock]
</description>
</method>
<method name="randi_range">
<return type="int">
</return>
<argument index="0" name="from" type="int">
</argument>
<argument index="1" name="to" type="int">
</argument>
<description>
Random range, any 32-bit integer value between [code]from[/code] and [code]to[/code] (inclusive). If [code]to[/code] is lesser than [code]from[/code] they are swapped.
akien-mga marked this conversation as resolved.
Show resolved Hide resolved
[codeblock]
print(randi_range(0, 1)) # Prints 0 or 1
print(randi_range(-10, 1000)) # Prints any number from -10 to 1000
[/codeblock]
</description>
</method>
Expand Down
24 changes: 18 additions & 6 deletions modules/gdscript/gdscript_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ const char *GDScriptFunctions::get_func_name(Function p_func) {
"randomize",
"randi",
"randf",
"rand_range",
"randf_range",
"randi_range",
"seed",
"rand_seed",
"deg2rad",
Expand Down Expand Up @@ -419,20 +420,26 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
Math::randomize();
r_ret = Variant();
} break;
case MATH_RAND: {
case MATH_RANDI: {
VALIDATE_ARG_COUNT(0);
r_ret = Math::rand();
} break;
case MATH_RANDF: {
VALIDATE_ARG_COUNT(0);
r_ret = Math::randf();
} break;
case MATH_RANDOM: {
case MATH_RANDF_RANGE: {
VALIDATE_ARG_COUNT(2);
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
r_ret = Math::random((double)*p_args[0], (double)*p_args[1]);
} break;
case MATH_RANDI_RANGE: {
VALIDATE_ARG_COUNT(2);
VALIDATE_ARG_NUM(0);
VALIDATE_ARG_NUM(1);
r_ret = Math::random((int)*p_args[0], (int)*p_args[1]);
} break;
case MATH_SEED: {
VALIDATE_ARG_COUNT(1);
VALIDATE_ARG_NUM(0);
Expand Down Expand Up @@ -1655,7 +1662,7 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
mi.return_val.type = Variant::NIL;
return mi;
} break;
case MATH_RAND: {
case MATH_RANDI: {
MethodInfo mi("randi");
mi.return_val.type = Variant::INT;
return mi;
Expand All @@ -1665,11 +1672,16 @@ MethodInfo GDScriptFunctions::get_info(Function p_func) {
mi.return_val.type = Variant::FLOAT;
return mi;
} break;
case MATH_RANDOM: {
MethodInfo mi("rand_range", PropertyInfo(Variant::FLOAT, "from"), PropertyInfo(Variant::FLOAT, "to"));
case MATH_RANDF_RANGE: {
MethodInfo mi("randf_range", PropertyInfo(Variant::FLOAT, "from"), PropertyInfo(Variant::FLOAT, "to"));
mi.return_val.type = Variant::FLOAT;
return mi;
} break;
case MATH_RANDI_RANGE: {
MethodInfo mi("randi_range", PropertyInfo(Variant::INT, "from"), PropertyInfo(Variant::INT, "to"));
mi.return_val.type = Variant::INT;
return mi;
} break;
case MATH_SEED: {
MethodInfo mi("seed", PropertyInfo(Variant::INT, "seed"));
mi.return_val.type = Variant::NIL;
Expand Down
5 changes: 3 additions & 2 deletions modules/gdscript/gdscript_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ class GDScriptFunctions {
MATH_MOVE_TOWARD,
MATH_DECTIME,
MATH_RANDOMIZE,
MATH_RAND,
MATH_RANDI,
MATH_RANDF,
MATH_RANDOM,
MATH_RANDF_RANGE,
MATH_RANDI_RANGE,
MATH_SEED,
MATH_RANDSEED,
MATH_DEG2RAD,
Expand Down
11 changes: 9 additions & 2 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/GD.cs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ public static void Randomize()

public static double RandRange(double from, double to)
{
return godot_icall_GD_rand_range(from, to);
return godot_icall_GD_randf_range(from, to);
}

public static int RandRange(int from, int to)
{
return godot_icall_GD_randi_range(from, to);
}

public static uint RandSeed(ulong seed, out ulong newSeed)
Expand Down Expand Up @@ -238,9 +243,11 @@ public static Variant.Type TypeToVariantType(Type type)
[MethodImpl(MethodImplOptions.InternalCall)]
internal extern static void godot_icall_GD_randomize();

[MethodImpl(MethodImplOptions.InternalCall)]
internal extern static double godot_icall_GD_randf_range(double from, double to);

[MethodImpl(MethodImplOptions.InternalCall)]
internal extern static double godot_icall_GD_rand_range(double from, double to);
internal extern static int godot_icall_GD_randi_range(int from, int to);
Chaosus marked this conversation as resolved.
Show resolved Hide resolved

[MethodImpl(MethodImplOptions.InternalCall)]
internal extern static uint godot_icall_GD_rand_seed(ulong seed, out ulong newSeed);
Expand Down
9 changes: 7 additions & 2 deletions modules/mono/glue/gd_glue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,11 @@ void godot_icall_GD_randomize() {
Math::randomize();
}

double godot_icall_GD_rand_range(double from, double to) {
double godot_icall_GD_randf_range(double from, double to) {
return Math::random(from, to);
}

int32_t godot_icall_GD_randi_range(int32_t from, int32_t to) {
return Math::random(from, to);
}

Expand Down Expand Up @@ -298,7 +302,8 @@ void godot_register_gd_icalls() {
mono_add_internal_call("Godot.GD::godot_icall_GD_randf", (void *)godot_icall_GD_randf);
mono_add_internal_call("Godot.GD::godot_icall_GD_randi", (void *)godot_icall_GD_randi);
mono_add_internal_call("Godot.GD::godot_icall_GD_randomize", (void *)godot_icall_GD_randomize);
mono_add_internal_call("Godot.GD::godot_icall_GD_rand_range", (void *)godot_icall_GD_rand_range);
mono_add_internal_call("Godot.GD::godot_icall_GD_randf_range", (void *)godot_icall_GD_randf_range);
mono_add_internal_call("Godot.GD::godot_icall_GD_randi_range", (void *)godot_icall_GD_randi_range);
mono_add_internal_call("Godot.GD::godot_icall_GD_rand_seed", (void *)godot_icall_GD_rand_seed);
mono_add_internal_call("Godot.GD::godot_icall_GD_seed", (void *)godot_icall_GD_seed);
mono_add_internal_call("Godot.GD::godot_icall_GD_str", (void *)godot_icall_GD_str);
Expand Down
Loading