Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,623 questions

55,358 answers

573 users

How to generate 3 no-digit repeated random integers, each with distinct digits in Rust

1 Answer

0 votes
use rand::rng; // thread‑local RNG (rand 0.9+)
use rand::seq::IndexedRandom; // needed for .choose() in rand 0.9+

// ------------------------------------------------------------
// Function: generate_number
// Purpose: Create one 3-digit integer with distinct digits,
//          drawn from a shared pool of available digits.
//          The first digit is never zero, so the result
//          is always a true 3-digit integer.
// ------------------------------------------------------------
fn generate_number(available_digits: &mut Vec<char>, length: usize) -> i32 {

    // Choose the first digit from the pool, excluding '0'
    let non_zero_digits: Vec<char> = available_digits
        .iter()
        .cloned()
        .filter(|d| *d != '0')
        .collect();

    let mut rng = rng(); // thread‑local RNG (rand 0.9+)
    let first_digit = *non_zero_digits.choose(&mut rng).unwrap();

    // Remove first digit from pool
    if let Some(pos) = available_digits.iter().position(|&d| d == first_digit) {
        available_digits.remove(pos);
    }

    // Choose the remaining digits from the updated pool
    let mut remaining: Vec<char> = Vec::new();
    for _ in 1..length {
        let d = *available_digits.choose(&mut rng).unwrap();
        remaining.push(d);

        if let Some(pos) = available_digits.iter().position(|&x| x == d) {
            available_digits.remove(pos);
        }
    }

    // Build the number as a string, then convert to int
    let mut digits_str = first_digit.to_string();
    for d in remaining {
        digits_str.push(d);
    }

    digits_str.parse::<i32>().unwrap()
}


// ------------------------------------------------------------
// Function: generate_three_distinct_digit_numbers
// Purpose: Produce three integers, each with distinct digits,
//          and no digit repeated across all three numbers.
//          All numbers are guaranteed to be 3 digits long.
// ------------------------------------------------------------
fn generate_three_distinct_digit_numbers() -> (i32, i32, i32) {

    // Start with all digits 0–9 as characters
    let mut digits: Vec<char> = (0..10).map(|i| char::from_digit(i, 10).unwrap()).collect();

    // Generate three numbers, each 3 digits long
    let n1 = generate_number(&mut digits, 3);
    let n2 = generate_number(&mut digits, 3);
    let n3 = generate_number(&mut digits, 3);

    (n1, n2, n3)
}


// ------------------------------------------------------------
// Main execution
// ------------------------------------------------------------
fn main() {

    // Run the generator 5 times
    for _ in 0..5 {
        let (n1, n2, n3) = generate_three_distinct_digit_numbers();
        println!("({}, {}, {})", n1, n2, n3);
    }
}


/*
run:

(345, 260, 879)
(527, 968, 143)
(743, 612, 580)
(247, 593, 108)
(360, 894, 125)

*/

 



answered Jul 17 by avibootz

Related questions

...