# base/seededrandom (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/base/seededrandom/

Seeded random number generator. (the original base/seededrandom.js, mulberry32)
- Deterministic random numbers that always produce the same sequence from the same seed. Used where reproducibility matters, such as stage generation, replays and versus sync.
- With the same seed as the original, the same sequence comes out bit for bit. The original's >>> 0, Math.imul and ^ are 32-bit integer operations, so
  they were ported as is with u32 wrapping_add / wrapping_mul / ^. (the intermediate mixed + imul(...) addition also becomes modulo 2^32 through ToInt32, so
  it equals wrapping_add) next_value is f64 as in the original. (it is a 32-bit value divided by 2^32, so it fits in f64 exactly)
- The seed and state are u32. The original (seed >>> 0) || 1 is the u32 seed as is, 1 if 0.
  Where the original used a negative number or one above 2^32 as a seed, converting that number to u32 and passing it gives the same sequence. (as u32 is the same as ToUint32)
- next_range takes f64 and returns f64, and computes in f64 like the original.
  next_int is i32 and computes in f64 like the original.
- pick returns `Option<&T>` (the original undefined is None), and shuffle shuffles a slice in place. (the original returns the same array)
- In the original, from_date reads the local year, month and day of a Date. Rust std has no local time, so it takes the year, month (1 ~ 12) and day.

## SeededRandom (struct) (Clone, Copy, Debug, PartialEq, Eq, Default)

Seeded random number generator. (mulberry32, the same sequence as the original for the same seed)

Example: Reproduce the same results with the same seed

```rust
use vanilla::SeededRandom;

let mut first_random = SeededRandom::new(12345);
let mut second_random = SeededRandom::new(12345);
for _ in 0..10 {
	assert_eq!(first_random.next_value(), second_random.next_value());
}
let mut random = SeededRandom::new(42);
let mut card_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
random.shuffle(&mut card_list);
assert_eq!(card_list, [0, 7, 3, 5, 2, 1, 8, 9, 4, 6]);
```

- `pub fn new(seed: u32) -> SeededRandom` (constructor, since 0.3.2): Constructor. (uses 1 if the seed is 0)
  - `seed` u32: Seed. (1 if 0)
  - returns SeededRandom: A generator with that seed as its state.
- `pub fn set_seed(&mut self, seed: u32)` (method, since 0.3.2): Resets the seed. (uses 1 if the seed is 0)
  - `seed` u32: New seed. (1 if 0)
- `pub fn get_state(&self) -> u32` (method, since 0.3.2): Returns the current internal state. (for saving and restoring)
  - returns u32: Internal state. (restoring it with set_state gives the same sequence afterward)
- `pub fn set_state(&mut self, state: u32)` (method, since 0.3.2): Restores the internal state. (0 is also accepted as is)
  - `state` u32: State taken with get_state.

  Example: Save the state and restore it

  ```rust
  use vanilla::SeededRandom;
  
  let mut random = SeededRandom::new(7);
  let saved_state = random.get_state();
  let first_value = random.next_value();
  random.set_state(saved_state);
  assert_eq!(random.next_value(), first_value);
  ```

- `pub fn next_value(&mut self) -> f64` (method, since 0.3.2): Returns a random number from 0 inclusive to 1 exclusive.
  - returns f64: A real number from 0 inclusive to 1 exclusive. (an f64 of a 32-bit value divided by 2^32; the state advances one step)
- `pub fn next_range(&mut self, min_value: f64, max_value: f64) -> f64` (method, since 0.3.2): Returns a random real number from min_value inclusive to max_value exclusive.
  - Rounding in the math can produce a value equal to max_value. (when next_value is very close to 1, same as the original)
  - `min_value` f64: Lower bound. (inclusive)
  - `max_value` f64: Upper bound. (exclusive)
  - returns f64: `min_value + (max_value - min_value) * next_value` computed in f64.
- `pub fn next_int(&mut self, min_value: i32, max_value: i32) -> i32` (method, since 0.3.2): Returns a random integer from min_value to max_value inclusive.
  - `min_value` i32: Lower bound. (inclusive)
  - `max_value` i32: Upper bound. (inclusive)
  - returns i32: An integer from min_value to max_value inclusive.

  Example: A die that includes both ends

  ```rust
  use vanilla::SeededRandom;
  
  let mut random = SeededRandom::new(777);
  let value_list: Vec<i32> = (0..10).map(|_| random.next_int(-5, 5)).collect();
  assert_eq!(value_list, vec![2, -5, -3, -4, 1, -1, -3, 3, 1, 3]);
  for _ in 0..100 {
  	let dice_value = random.next_int(1, 6);
  	assert!((1..=6).contains(&dice_value));
  }
  ```

- `pub fn pick<'a, T>(&mut self, array: &'a [T]) -> Option<&'a T>` (method, since 0.3.2): Picks one from an array. (None if empty)
  - `array` &'a [T]: Slice to pick from.
  - returns Option<&'a T>: Reference to the picked item. None if empty. (no random number is used if empty)

  Example: Pick one reward

  ```rust
  use vanilla::SeededRandom;
  
  let mut random = SeededRandom::new(42);
  let reward_list = ["gold", "gem", "potion"];
  let reward = random.pick(&reward_list);
  assert!(reward.is_some_and(|reward| reward_list.contains(reward)));
  let empty_list: [&str; 0] = [];
  assert_eq!(random.pick(&empty_list), None);
  ```

- `pub fn shuffle<T>(&mut self, array: &mut [T])` (method, since 0.3.2): Shuffles an array in place. (Fisher-Yates, uses random numbers in the same order as the original)
  - `array` &mut [T]: Slice to shuffle. (changed in place, uses random numbers length - 1 times)
- `pub fn create_daily_seed(date_number: u32, base_seed: u32) -> u32` (static, since 0.3.2): Creates a date-based seed. (mixes a YYYYMMDD integer like 20260831 so the same date gives the same seed everywhere, 1 if 0)
  - `date_number` u32: Date integer in YYYYMMDD form.
  - `base_seed` u32: Base seed to mix in. (the original default is DAILY_BASE_SEED; giving a different one per game gives a different seed even on the same day)
  - returns u32: Seed. (1 if the mixed value is 0)

  Example: Today's seed

  ```rust
  use vanilla::base::seededrandom::DAILY_BASE_SEED;
  use vanilla::SeededRandom;
  
  assert_eq!(SeededRandom::create_daily_seed(20261117, DAILY_BASE_SEED), 1951175260);
  assert_eq!(SeededRandom::create_daily_seed(20260831, 12345), 679594006);
  ```

- `pub fn from_date(year: i32, month: u32, day: u32, base_seed: u32) -> SeededRandom` (static, since 0.3.2): Creates a seeded random for a date. (month is 1 ~ 12, in place of the original getMonth() + 1)
  - `year` i32: Year. (like 2026)
  - `month` u32: Month. (1 ~ 12)
  - `day` u32: Day. (1 ~ 31)
  - `base_seed` u32: Base seed to mix in. (same as create_daily_seed)
  - returns SeededRandom: A generator seeded with `create_daily_seed(year * 10000 + month * 100 + day, base_seed)`.

  Example: Make a daily challenge board

  ```rust
  use vanilla::base::seededrandom::DAILY_BASE_SEED;
  use vanilla::SeededRandom;
  
  let mut first_random = SeededRandom::from_date(2026, 11, 17, DAILY_BASE_SEED);
  let mut second_random = SeededRandom::from_date(2026, 11, 17, DAILY_BASE_SEED);
  assert_eq!(first_random.get_state(), 1951175260);
  let mut first_board = [1, 2, 3, 4, 5, 6, 7, 8, 9];
  let mut second_board = first_board;
  first_random.shuffle(&mut first_board);
  second_random.shuffle(&mut second_board);
  assert_eq!(first_board, second_board);
  ```

## Functions and constants

- `pub const DAILY_BASE_SEED: u32 = 0x9E3779B1` (field, since 0.3.2): Default value mixed into date seeds. (the default base_seed argument of the original createDailySeed / from_date)
  - returns u32
