vanilladocs
vanillapunch.com
API / base/seededrandom

base/seededrandom

시드 난수 생성기. (원본 base/seededrandom.js, mulberry32)

  • 같은 시드에서 항상 같은 수열이 나오는 결정론적 난수다. 스테이지 생성, 리플레이, 대전 동기화처럼 재현이 필요한 곳에 쓴다.
  • 원본과 같은 시드면 같은 수열이 비트 단위로 똑같이 나온다. 원본의 >>> 0, Math.imul, ^ 는 32 비트 정수 연산이라 u32 의 wrapping_add / wrapping_mul / ^ 로 그대로 옮겼다. (중간의 mixed + imul(...) 덧셈도 ToInt32 를 거치면 2^32 나머지라 wrapping_add 와 같다) next_value 는 원본처럼 f64 다. (32 비트 값을 2^32 로 나눈 것이라 f64 에 그대로 담긴다)
  • 시드와 상태는 u32 다. 원본 (seed >>> 0) || 1 은 u32 시드 그대로, 0 이면 1 이다. 원본에서 음수나 2^32 넘는 수를 시드로 쓰던 곳은 그 수를 u32 로 바꿔 넘기면 같은 수열이 나온다. (as u32 는 ToUint32 와 같다)
  • next_range 는 f64 를 받아 f64 를 돌려주고, 계산은 원본처럼 f64 다. next_int 는 i32 이고 계산은 원본처럼 f64 다.
  • pick 은 Option<&T>(원본 undefined 는 None), shuffle 은 슬라이스를 제자리에서 섞는다. (원본은 같은 배열을 돌려준다)
  • from_date 는 원본이 Date 의 지역 시각 연, 월, 일을 읽는다. 러스트 std 에는 지역 시각이 없어서 연, 월(1 ~ 12), 일을 받는다.

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.

SeededRandomstruct traits Clone, Copy, Debug, PartialEq, Eq, Default

짝Counterpart: vanilla.js SeededRandom소스source

시드 난수 생성기. (mulberry32, 같은 시드면 원본과 같은 수열)

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

예제Example같은 시드로 같은 결과 다시 내기Reproduce the same results with the same seed
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]);
생성자constructorpub fn new(seed: u32) -> SeededRandom#0.3.2 부터since 0.3.2소스source

생성. (시드가 0 이면 1 을 쓴다)

Constructor. (uses 1 if the seed is 0)

seedu32
시드. (0 이면 1)Seed. (1 if 0)

반환ReturnsSeededRandom 그 시드를 상태로 둔 생성기.A generator with that seed as its state.

짝Counterpart: vanilla.js SeededRandom.constructor

정적staticpub fn create_daily_seed(date_number: u32, base_seed: u32) -> u32#0.3.2 부터since 0.3.2소스source

날짜 기반 시드를 만든다. (20260831 같은 YYYYMMDD 정수를 섞어, 같은 날짜면 어디서나 같은 시드가 나온다, 0 이면 1)

Creates a date-based seed. (mixes a YYYYMMDD integer like 20260831 so the same date gives the same seed everywhere, 1 if 0)

date_numberu32
YYYYMMDD 꼴의 날짜 정수.Date integer in YYYYMMDD form.
base_seedu32
섞을 바탕 시드. (원본 기본값은 DAILY_BASE_SEED, 게임마다 다르게 주면 같은 날에도 다른 시드가 나온다)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)

반환Returnsu32 시드. (섞은 값이 0 이면 1)Seed. (1 if the mixed value is 0)

짝Counterpart: vanilla.js SeededRandom.createDailySeed

예제Example오늘의 시드Today's seed
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);
정적staticpub fn from_date(year: i32, month: u32, day: u32, base_seed: u32) -> SeededRandom#0.3.2 부터since 0.3.2소스source

날짜의 시드 난수를 만든다. (month 는 1 ~ 12, 원본 getMonth() + 1 자리)

Creates a seeded random for a date. (month is 1 ~ 12, in place of the original getMonth() + 1)

yeari32
연. (2026 같은)Year. (like 2026)
monthu32
월. (1 ~ 12)Month. (1 ~ 12)
dayu32
일. (1 ~ 31)Day. (1 ~ 31)
base_seedu32
섞을 바탕 시드. (create_daily_seed 와 같다)Base seed to mix in. (same as create_daily_seed)

반환ReturnsSeededRandom create_daily_seed(year * 10000 + month * 100 + day, base_seed) 를 시드로 만든 생성기.A generator seeded with create_daily_seed(year * 10000 + month * 100 + day, base_seed).

짝Counterpart: vanilla.js SeededRandom.fromDate

예제Example데일리 챌린지 보드 만들기Make a daily challenge board
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);
메서드methodpub fn set_seed(&mut self, seed: u32)#0.3.2 부터since 0.3.2소스source

시드 재설정. (시드가 0 이면 1 을 쓴다)

Resets the seed. (uses 1 if the seed is 0)

seedu32
새 시드. (0 이면 1)New seed. (1 if 0)

짝Counterpart: vanilla.js SeededRandom.setSeed

메서드methodpub fn get_state(&self) -> u32#0.3.2 부터since 0.3.2소스source

현재 내부 상태 반환. (저장, 복원용)

Returns the current internal state. (for saving and restoring)

반환Returnsu32 내부 상태. (set_state 로 되돌리면 그 뒤 수열이 같다)Internal state. (restoring it with set_state gives the same sequence afterward)

짝Counterpart: vanilla.js SeededRandom.getState

메서드methodpub fn set_state(&mut self, state: u32)#0.3.2 부터since 0.3.2소스source

내부 상태 복원. (0 도 그대로 받는다)

Restores the internal state. (0 is also accepted as is)

stateu32
get_state 로 받아 둔 상태.State taken with get_state.

짝Counterpart: vanilla.js SeededRandom.setState

예제Example상태를 저장했다가 되돌리기Save the state and restore it
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);
메서드methodpub fn next_value(&mut self) -> f64#0.3.2 부터since 0.3.2소스source

0 이상 1 미만의 난수를 돌려준다.

Returns a random number from 0 inclusive to 1 exclusive.

반환Returnsf64 0 이상 1 미만의 실수. (32 비트 값을 2^32 로 나눈 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)

짝Counterpart: vanilla.js SeededRandom.nextValue

메서드methodpub fn next_range(&mut self, min_value: f64, max_value: f64) -> f64#0.3.2 부터since 0.3.2소스source

min_value 이상 max_value 미만의 실수 난수를 돌려준다.

  • 셈의 반올림으로 max_value 와 같은 값이 나올 수 있다. (next_value 가 1 에 아주 가까울 때, 원본과 같다)

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_valuef64
아래 끝. (포함)Lower bound. (inclusive)
max_valuef64
위 끝. (포함하지 않는다)Upper bound. (exclusive)

반환Returnsf64 min_value + (max_value - min_value) * next_value 를 f64 로 셈한 값.min_value + (max_value - min_value) * next_value computed in f64.

짝Counterpart: vanilla.js SeededRandom.nextRange

메서드methodpub fn next_int(&mut self, min_value: i32, max_value: i32) -> i32#0.3.2 부터since 0.3.2소스source

min_value 이상 max_value 이하의 정수 난수를 돌려준다.

Returns a random integer from min_value to max_value inclusive.

min_valuei32
아래 끝. (포함)Lower bound. (inclusive)
max_valuei32
위 끝. (포함)Upper bound. (inclusive)

반환Returnsi32 min_value 이상 max_value 이하의 정수.An integer from min_value to max_value inclusive.

짝Counterpart: vanilla.js SeededRandom.nextInt

예제Example양 끝을 모두 포함하는 주사위A die that includes both ends
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));
}
메서드methodpub fn pick<'a, T>(&mut self, array: &'a [T]) -> Option<&'a T>#0.3.2 부터since 0.3.2소스source

배열에서 하나 뽑는다. (비어 있으면 None)

Picks one from an array. (None if empty)

array&'a [T]
뽑을 슬라이스.Slice to pick from.

반환ReturnsOption<&'a T> 뽑은 항목의 참조. 비었으면 None. (비었으면 난수를 쓰지 않는다)Reference to the picked item. None if empty. (no random number is used if empty)

짝Counterpart: vanilla.js SeededRandom.pick

예제Example보상 하나 고르기Pick one reward
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);
메서드methodpub fn shuffle<T>(&mut self, array: &mut [T])#0.3.2 부터since 0.3.2소스source

배열을 제자리에서 섞는다. (Fisher-Yates, 원본과 같은 순서로 난수를 쓴다)

Shuffles an array in place. (Fisher-Yates, uses random numbers in the same order as the original)

array&mut [T]
섞을 슬라이스. (그 자리에서 바뀐다, 길이 - 1 번 난수를 쓴다)Slice to shuffle. (changed in place, uses random numbers length - 1 times)

짝Counterpart: vanilla.js SeededRandom.shuffle

상수Constants

상수constantpub const DAILY_BASE_SEED: u32 = 0x9E3779B1#0.3.2 부터since 0.3.2소스source

날짜 시드에 섞는 기본 값. (원본 createDailySeed / from_date 의 base_seed 기본 인자)

Default value mixed into date seeds. (the default base_seed argument of the original createDailySeed / from_date)

반환Returnsu32