v / vlib / rand / config / config.v
49 lines · 44 sloc · 1.65 KB · 763f94388b1ceff59c3e68ebda2c9f57197f32a4
Raw
1// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
2// Use of this source code is governed by an MIT license
3// that can be found in the LICENSE file.
4module config
5
6import rand.seed
7
8// PRNGConfigStruct is a configuration struct for creating a new instance of the default RNG.
9// Note that the RNGs may have a different number of u32s required for seeding. The default
10// generator WyRand used 64 bits, ie. 2 u32s so that is the default. In case your desired generator
11// uses a different number of u32s, use the `seed.time_seed_array()` method with the correct
12// number of u32s.
13@[params]
14pub struct PRNGConfigStruct {
15pub:
16 seed_ []u32 = seed.time_seed_array(2)
17}
18
19// Configuration struct for generating normally distributed floats. The default value for
20// `mu` is 0 and the default value for `sigma` is 1.
21@[params]
22pub struct NormalConfigStruct {
23pub:
24 mu f64 = 0.0
25 sigma f64 = 1.0
26}
27
28// Configuration struct for the shuffle functions.
29// The start index is inclusive and the end index is exclusive.
30// Set the end to 0 to shuffle until the end of the array.
31@[params]
32pub struct ShuffleConfigStruct {
33pub:
34 start int
35 end int
36}
37
38// validate_for is a helper function for validating the configuration struct for the given array.
39pub fn (config ShuffleConfigStruct) validate_for[T](a []T) ! {
40 if config.start < 0 || config.start >= a.len {
41 return error("argument 'config.start' must be in range [0, a.len)")
42 }
43 if config.end < 0 || config.end > a.len {
44 return error("argument 'config.end' must be in range [0, a.len]")
45 }
46 if config.end != 0 && config.end <= config.start {
47 return error("argument 'config.end' must be greater than 'config.start'")
48 }
49}
50