@@ -33,9 +33,6 @@ Global scope methods are easier to set up, but they don't offer as much control.
3333RandomNumberGenerator requires more code to use, but allows creating
3434multiple instances, each with their own seed and state.
3535
36- This tutorial uses global scope methods, except when the method only exists in
37- the RandomNumberGenerator class.
38-
3936The randomize() method
4037----------------------
4138
@@ -134,7 +131,7 @@ number between 0 and 1. This is useful to implement a
134131:ref: `doc_random_number_generation_weighted_random_probability ` system, among
135132other things.
136133
137- :ref: `randfn() <class_RandomNumberGenerator_method_randfn >` returns a random
134+ :ref: `randfn() <class_@GlobalScope_method_randfn >` returns a random
138135floating-point number following a `normal distribution
139136<https://en.wikipedia.org/wiki/Normal_distribution> `__. This means the returned
140137value is more likely to be around the mean (0.0 by default),
@@ -144,16 +141,12 @@ varying by the deviation (1.0 by default):
144141 .. code-tab :: gdscript GDScript
145142
146143 # Prints a random floating-point number from a normal distribution with a mean 0.0 and deviation 1.0.
147- var random = RandomNumberGenerator.new()
148- random.randomize()
149- print(random.randfn())
144+ print(randfn())
150145
151146 .. code-tab :: csharp
152147
153148 // Prints a normally distributed floating-point number between 0.0 and 1.0.
154- var random = new RandomNumberGenerator();
155- random.Randomize();
156- GD.Print(random.Randfn());
149+ GD.Print(GD.Randfn());
157150
158151:ref: `randf_range() <class_@GlobalScope_method_randf_range >` takes two arguments
159152``from `` and ``to ``, and returns a random floating-point number between ``from ``
@@ -165,41 +158,46 @@ and ``to``:
165158 # Prints a random floating-point number between -4 and 6.5.
166159 print(randf_range(-4, 6.5))
167160
168- :ref: `RandomNumberGenerator.randi_range()
169- <class_RandomNumberGenerator_method_randi_range>` takes two arguments ``from ``
161+ .. code-tab :: csharp
162+
163+ // Prints a random floating-point number between -4 and 6.5.
164+ GD.Print(GD.RandfRange(-4, 6.5));
165+
166+ :ref: `randi_range() <class_@GlobalScope_method_randi_range >` takes two arguments ``from ``
170167and ``to ``, and returns a random integer between ``from `` and ``to ``:
171168
172169.. tabs ::
173170 .. code-tab :: gdscript GDScript
174171
175172 # Prints a random integer between -10 and 10.
176- var random = RandomNumberGenerator.new()
177- random.randomize()
178- print(random.randi_range(-10, 10))
173+ print(randi_range(-10, 10))
179174
180175 .. code-tab :: csharp
181176
182177 // Prints a random integer number between -10 and 10.
183- random.Randomize();
184- GD.Print(random.RandiRange(-10, 10));
178+ GD.Print(GD.RandiRange(-10, 10));
185179
186180Get a random array element
187181--------------------------
188182
189- We can use random integer generation to get a random element from an array:
183+ We can use random integer generation to get a random element from an array,
184+ or use the :ref: `Array.pick_random<class_Array_method_pick_random> ` method
185+ to do it for us:
190186
191187.. tabs ::
192188 .. code-tab :: gdscript GDScript
193189
194190 var _fruits = ["apple", "orange", "pear", "banana"]
195191
196192 func _ready():
197- randomize()
198-
199193 for i in range(100):
200194 # Pick 100 fruits randomly.
201195 print(get_fruit())
202196
197+ for i in range(100):
198+ # Pick 100 fruits randomly, this time using the `Array.pick_random() `
199+ # helper method. This has the same behavior as `get_fruit() `.
200+ print(_fruits.pick_random())
203201
204202 func get_fruit():
205203 var random_fruit = _fruits[randi() % _fruits.size()]
@@ -209,29 +207,37 @@ We can use random integer generation to get a random element from an array:
209207
210208 .. code-tab :: csharp
211209
212- private string[] _fruits = { "apple", "orange", "pear", "banana" };
210+ // Use Godot's Array type instead of a BCL type so we can use `PickRandom() ` on it.
211+ private Godot.Collections.Array<string> _fruits = new Godot.Collections.Array<string> { "apple", "orange", "pear", "banana" };
213212
214213 public override void _Ready()
215214 {
216- GD.Randomize();
217-
218215 for (int i = 0; i < 100; i++)
219216 {
220217 // Pick 100 fruits randomly.
221218 GD.Print(GetFruit());
222219 }
220+
221+ for (int i = 0; i < 100; i++)
222+ {
223+ // Pick 100 fruits randomly, this time using the `Array.PickRandom() `
224+ // helper method. This has the same behavior as `GetFruit() `.
225+ GD.Print(_fruits.PickRandom());
226+ }
223227 }
224228
225229 public string GetFruit()
226230 {
227- string randomFruit = _fruits[GD.Randi() % _fruits.Length ];
231+ string randomFruit = _fruits[GD.Randi() % _fruits.Size() ];
228232 // Returns "apple", "orange", "pear", or "banana" every time the code runs.
229233 // We may get the same fruit multiple times in a row.
230234 return randomFruit;
231235 }
232236
233237To prevent the same fruit from being picked more than once in a row, we can add
234- more logic to this method:
238+ more logic to the above method. In this case, we can't use
239+ :ref: `Array.pick_random<class_Array_method_pick_random> ` since it lacks a way to
240+ prevent repetition:
235241
236242.. tabs ::
237243 .. code-tab :: gdscript GDScript
@@ -241,8 +247,6 @@ more logic to this method:
241247
242248
243249 func _ready():
244- randomize()
245-
246250 # Pick 100 fruits randomly.
247251 for i in range(100):
248252 print(get_fruit())
@@ -270,8 +274,6 @@ more logic to this method:
270274
271275 public override void _Ready()
272276 {
273- GD.Randomize();
274-
275277 for (int i = 0; i < 100; i++)
276278 {
277279 // Pick 100 fruits randomly.
@@ -316,8 +318,6 @@ We can apply similar logic from arrays to dictionaries as well:
316318
317319
318320 func _ready():
319- randomize()
320-
321321 for i in range(20):
322322 print(get_metal())
323323
@@ -341,8 +341,6 @@ floating-point number between 0.0 and 1.0. We can use this to create a
341341 .. code-tab :: gdscript GDScript
342342
343343 func _ready():
344- randomize()
345-
346344 for i in range(100):
347345 print(get_item_rarity())
348346
@@ -364,8 +362,6 @@ floating-point number between 0.0 and 1.0. We can use this to create a
364362
365363 public override void _Ready()
366364 {
367- GD.Randomize();
368-
369365 for (int i = 0; i < 100; i++)
370366 {
371367 GD.Print(GetItemRarity());
@@ -413,7 +409,6 @@ ends up empty. When that happens, you reinitialize it to its default value::
413409
414410
415411 func _ready():
416- randomize()
417412 _fruits_full = _fruits.duplicate()
418413 _fruits.shuffle()
419414
@@ -456,7 +451,6 @@ terrain. Godot provides :ref:`class_fastnoiselite` for this, which supports
456451 var _noise = FastNoiseLite.new()
457452
458453 func _ready():
459- randomize()
460454 # Configure the FastNoiseLite instance.
461455 _noise.noise_type = FastNoiseLite.NoiseType.TYPE_SIMPLEX_SMOOTH
462456 _noise.seed = randi()
@@ -474,7 +468,6 @@ terrain. Godot provides :ref:`class_fastnoiselite` for this, which supports
474468
475469 public override void _Ready()
476470 {
477- GD.Randomize();
478471 // Configure the FastNoiseLite instance.
479472 _noise.NoiseType = NoiseTypeEnum.SimplexSmooth;
480473 _noise.Seed = (int)GD.Randi();
0 commit comments