# 随机数生成¶

## 全局作用域 vs RandomNumberGenerator 类¶

Godot 提供了两种生成随机数的方式：通过全局作用域方法或使用 RandomNumberGenerator 类。

RandomNumberGenerator则需要写更多代码, 但提供许多在全局作用域内找不到的方法, 如 randi_range()randfn() . 除此之外, 它还允许创建多个实例, 每个实例都有自己的种子.

## randomize() 方法¶

```func _ready():
randomize()
```

```func _ready():
seed(12345)
# To use a string as a seed, you can hash it to a number.
seed("Hello world".hash())
```

```var random = RandomNumberGenerator.new()
random.randomize()
```

## 获得一个随机数¶

```# Prints a random integer between 0 and 49.
print(randi() % 50)

# Prints a random integer between 10 and 60.
print(randi() % 51 + 10)
```

randf() 返回一个0到1之间的随机浮点数. 在实现 加权随机概率 系统等时非常有用.

randfn() 返回遵循 正态分布 的随机浮点数。这意味着返回值更有可能在平均值附近（默认为 0.0），随偏差变化（默认为 1.0）:

```# Prints a random floating-point number from a normal distribution with a mean 0.0 and deviation 1.0.
var random = RandomNumberGenerator.new()
random.randomize()
print(random.randfn())
```

rand_range() 接受两个参数 `from``to` ，并返回一个介于 `from``to` 之间的随机浮点数:

```# Prints a random floating-point number between -4 and 6.5.
print(rand_range(-4, 6.5))
```

RandomNumberGenerator.randi_range() 接受两个参数 `from``to` ，并返回一个介于 `from``to` 之间的随机整数：

```# Prints a random integer between -10 and 10.
var random = RandomNumberGenerator.new()
random.randomize()
print(random.randi_range(-10, 10))
```

## 获取一个随机数组元素¶

```var _fruits = ["apple", "orange", "pear", "banana"]

randomize()

for i in range(100):
# Pick 100 fruits randomly.
print(get_fruit())

func get_fruit():
var random_fruit = _fruits[randi() % _fruits.size()]
# Returns "apple", "orange", "pear", or "banana" every time the code runs.
# We may get the same fruit multiple times in a row.
return random_fruit
```

```var _fruits = ["apple", "orange", "pear", "banana"]
var _last_fruit = ""

randomize()

# Pick 100 fruits randomly.
for i in range(100):
print(get_fruit())

func get_fruit():
var random_fruit = _fruits[randi() % _fruits.size()]
while random_fruit == _last_fruit:
# The last fruit was picked, try again until we get a different fruit.
random_fruit = _fruits[randi() % _fruits.size()]

# Note: if the random element to pick is passed by reference,
# such as an array or dictionary,
# use `_last_fruit = random_fruit.duplicate()` instead.
_last_fruit = random_fruit

# Returns "apple", "orange", "pear", or "banana" every time the code runs.
# The function will never return the same fruit more than once in a row.
return random_fruit
```

## 获取一个随机字典值¶

```var metals = {
"copper": {"quantity": 50, "price": 50},
"silver": {"quantity": 20, "price": 150},
"gold": {"quantity": 3, "price": 500},
}

randomize()

for i in range(20):
print(get_metal())

func get_metal():
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
```

## 加权随机概率¶

randf() 方法返回一个介于 0.0 和 1.0 之间的浮点数。我们可以使用它来创建“加权”概率，其中不同的结果具有不同的可能性：

```func _ready():
randomize()

for i in range(100):
print(get_item_rarity())

func get_item_rarity():
var random_float = randf()

if random_float < 0.8:
# 80% chance of being returned.
return "Common"
elif random_float < 0.95:
# 15% chance of being returned.
return "Uncommon"
else:
# 5% chance of being returned.
return "Rare"
```

## 使用 shuffle bag 达到“更好”随机性¶

```var _fruits = ["apple", "orange", "pear", "banana"]
# A copy of the fruits array so we can restore the original value into `fruits`.
var _fruits_full = []

randomize()
_fruits_full = _fruits.duplicate()
_fruits.shuffle()

for i in 100:
print(get_fruit())

func get_fruit():
if _fruits.empty():
# Fill the fruits array again and shuffle it.
_fruits = _fruits_full.duplicate()
_fruits.shuffle()

# 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.
return random_fruit
```

## 随机噪音¶

```var _noise = OpenSimplexNoise.new()