oxedyne/fe2o3/fe2o3_social/src/culture.rs
1.5 KiB, 10 runs
created by r1870400018:6215, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | use strum::Display; |
| 2 | |
| 3 | #[derive(Clone, Copy, Debug, Display, Eq, PartialEq)] |
| 4 | #[repr(usize)] |
| 5 | pub enum Gender { |
| 6 | Male, |
| 7 | Female, |
| 8 | NonBinary, |
| 9 | Other, |
| 10 | PreferNotToSay, |
| 11 | } |
| 12 | |
| 13 | impl Gender { |
| 14 | /// Returns a random Gender variant with specified minority probability. |
| 15 | /// |
| 16 | /// # Arguments |
| 17 | /// * `p` - Probability (0.0 to 1.0) of selecting a minority gender (NonBinary, Other, PreferNotToSay). |
| 18 | /// The probability of Male or Female is (1-p), split equally between them. |
| 19 | /// |
| 20 | /// # Example |
| 21 | /// ``` |
| 22 | /// use oxedyne_fe2o3_social::culture::Gender; |
| 23 | /// |
| 24 | /// // 10% chance of minority gender, 90% chance of Male/Female. |
| 25 | /// let gender = Gender::rand_minority(0.1); |
| 26 | /// |
| 27 | /// // 50% chance of minority gender, 50% chance of Male/Female. |
| 28 | /// let gender = Gender::rand_minority(0.5); |
| 29 | /// ``` |
| 30 | pub fn rand_minority(p: f64) -> Self { |
| 31 | use ::oxedyne_fe2o3_core::rand::Rand; |
| 32 | |
| 33 | let random_val: f64 = Rand::value(); |
| 34 | |
| 35 | if random_val < (1.0 - p) { |
| 36 | // Choose Male or Female with equal probability. |
| 37 | if Rand::value::<f64>() < 0.5 { |
| 38 | Self::Male |
| 39 | } else { |
| 40 | Self::Female |
| 41 | } |
| 42 | } else { |
| 43 | // Choose from minority genders. |
| 44 | let minority_choice = Rand::in_range(0, 2); // 0, 1, or 2 |
| 45 | match minority_choice { |
| 46 | 0 => Self::NonBinary, |
| 47 | 1 => Self::Other, |
| 48 | 2 => Self::PreferNotToSay, |
| 49 | _ => unreachable!(), |
| 50 | } |
| 51 | } |
| 52 | } |
| 53 | } |