Skip to content

Commit

Permalink
Add C# code to random_number_generation and fix shuffle bag example. (g…
Browse files Browse the repository at this point in the history
…odotengine#8072)

* Add C# code to random_number_generation and fix shuffle bag example.

Changes made to shuffle bag example:
- Replace .empty() with .is_empty()
- More descriptive comment
- Change code-block into a tabbed code block

---------

Co-authored-by: Paul Joannon <[email protected]>
Co-authored-by: Raul Santos <[email protected]>
Co-authored-by: tetrapod <[email protected]>
  • Loading branch information
4 people authored Nov 20, 2024
1 parent 1b51bb7 commit 892322b
Showing 1 changed file with 75 additions and 11 deletions.
86 changes: 75 additions & 11 deletions tutorials/math/random_number_generation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -145,12 +145,12 @@ varying by the deviation (1.0 by default):
.. code-tab:: gdscript GDScript

# Prints a random floating-point number from a normal distribution with a mean 0.0 and deviation 1.0.
print(randfn())
print(randfn(0.0, 1.0))

.. code-tab:: csharp

// Prints a random floating-point number from a normal distribution with a mean of 0.0 and deviation of 1.0.
GD.Print(GD.Randfn());
// Prints a random floating-point number from a normal distribution with a mean 0.0 and deviation 1.0.
GD.Print(GD.Randfn(0.0, 1.0));

:ref:`randf_range() <class_@GlobalScope_method_randf_range>` takes two arguments
``from`` and ``to``, and returns a random floating-point number between ``from``
Expand All @@ -165,7 +165,7 @@ and ``to``:
.. code-tab:: csharp

// Prints a random floating-point number between -4 and 6.5.
GD.Print(GD.RandfRange(-4, 6.5));
GD.Print(GD.RandRange(-4.0, 6.5));

:ref:`randi_range() <class_@GlobalScope_method_randi_range>` takes two arguments ``from``
and ``to``, and returns a random integer between ``from`` and ``to``:
Expand All @@ -178,8 +178,8 @@ and ``to``, and returns a random integer between ``from`` and ``to``:

.. code-tab:: csharp

// Prints a random integer between -10 and 10.
GD.Print(GD.RandiRange(-10, 10));
// Prints a random integer number between -10 and 10.
GD.Print(GD.RandRange(-10, 10));

Get a random array element
--------------------------
Expand Down Expand Up @@ -314,7 +314,7 @@ We can apply similar logic from arrays to dictionaries as well:
.. tabs::
.. code-tab:: gdscript GDScript

var metals = {
var _metals = {
"copper": {"quantity": 50, "price": 50},
"silver": {"quantity": 20, "price": 150},
"gold": {"quantity": 3, "price": 500},
Expand All @@ -327,11 +327,36 @@ We can apply similar logic from arrays to dictionaries as well:


func get_metal():
var random_metal = metals.values()[randi() % metals.size()]
var random_metal = _metals.values()[randi() % metals.size()]
# Returns a random metal value dictionary every time the code runs.
# The same metal may be selected multiple times in succession.
return random_metal

.. code-tab:: csharp

private Godot.Collections.Dictionary<string, Godot.Collections.Dictionary<string, int>> _metals = new()
{
{"copper", new Godot.Collections.Dictionary<string, int>{{"quantity", 50}, {"price", 50}}},
{"silver", new Godot.Collections.Dictionary<string, int>{{"quantity", 20}, {"price", 150}}},
{"gold", new Godot.Collections.Dictionary<string, int>{{"quantity", 3}, {"price", 500}}},
};

public override void _Ready()
{
for (int i = 0; i < 20; i++)
{
GD.Print(GetMetal());
}
}

public Godot.Collections.Dictionary<string, int> GetMetal()
{
var (_, randomMetal) = _metals.ElementAt((int)(GD.Randi() % _metals.Count));
// Returns a random metal value dictionary every time the code runs.
// The same metal may be selected multiple times in succession.
return randomMetal;
}

.. _doc_random_number_generation_weighted_random_probability:

Weighted random probability
Expand Down Expand Up @@ -443,7 +468,10 @@ could get the same fruit three or more times in a row.

You can accomplish this using the *shuffle bag* pattern. It works by removing an
element from the array after choosing it. After multiple selections, the array
ends up empty. When that happens, you reinitialize it to its default value::
ends up empty. When that happens, you reinitialize it to its default value:

.. tabs::
.. code-tab:: gdscript GDScript

var _fruits = ["apple", "orange", "pear", "banana"]
# A copy of the fruits array so we can restore the original value into `fruits`.
Expand All @@ -467,9 +495,45 @@ ends up empty. When that happens, you reinitialize it to its default value::
# Get a random fruit, since we shuffled the array,
# and remove it from the `_fruits` array.
var random_fruit = _fruits.pop_front()
# Prints "apple", "orange", "pear", or "banana" every time the code runs.
# Returns "apple", "orange", "pear", or "banana" every time the code runs, removing it from the array.
# When all fruit are removed, it refills the array.
return random_fruit

.. code-tab:: csharp

private Godot.Collections.Array<string> _fruits = new() { "apple", "orange", "pear", "banana" };
// A copy of the fruits array so we can restore the original value into `fruits`.
private Godot.Collections.Array<string> _fruitsFull;

public override void _Ready()
{
_fruitsFull = _fruits.Duplicate();
_fruits.Shuffle();

for (int i = 0; i < 100; i++)
{
GD.Print(GetFruit());
}
}

public string GetFruit()
{
if(_fruits.Count == 0)
{
// Fill the fruits array again and shuffle it.
_fruits = _fruitsFull.Duplicate();
_fruits.Shuffle();
}

// Get a random fruit, since we shuffled the array,
string randomFruit = _fruits[0];
// and remove it from the `_fruits` array.
_fruits.RemoveAt(0);
// Returns "apple", "orange", "pear", or "banana" every time the code runs, removing it from the array.
// When all fruit are removed, it refills the array.
return randomFruit;
}

When running the above code, there is a chance to get the same fruit twice in a
row. Once we picked a fruit, it will no longer be a possible return value unless
the array is now empty. When the array is empty, we reset it back to its default
Expand Down Expand Up @@ -511,7 +575,7 @@ terrain. Godot provides :ref:`class_fastnoiselite` for this, which supports
public override void _Ready()
{
// Configure the FastNoiseLite instance.
_noise.NoiseType = NoiseTypeEnum.SimplexSmooth;
_noise.NoiseType = FastNoiseLite.NoiseTypeEnum.SimplexSmooth;
_noise.Seed = (int)GD.Randi();
_noise.FractalOctaves = 4;
_noise.Frequency = 1.0f / 20.0f;
Expand Down

0 comments on commit 892322b

Please sign in to comment.