ADVERTISEMENT
ADVERTISEMENT

Rust program to generate random values of a custom types

Rust | Random Numbers Example: Write a program to generate random values of a custom types.
Submitted by Nidhi, on November 13, 2021

Problem Solution:

In this program, we will generate random values of a user-defined structure, and print the result on the console screen.

Add random external library to your project

  1. Create your project using the below command.
    $cargo new random --bin
  2. Goto the project folder cd random and edit Cargo.toml file.
    $random>nano Cargo.toml
  3. Then add dependency in Cargo.toml file
    [dependencies]
    rand = "0.5.5"
  4. After that, build your project using the below command
    $random>cargo build
  5. Then execute your project after modification in the src/main.rs source file.
    $random>cargo run

Program/Source Code:

The source code to generate random values of a custom type is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.

// Rust program to generate random 
// values of a custom type

use rand::Rng;
use rand::distributions::{Distribution, Standard};

#[derive(Debug)]
struct MyType {
    num1: i16,
    num2: i16,
}

impl Distribution<MyType> for Standard {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> MyType {
        let (rand_num1, rand_num2) = rng.gen();
        MyType {
            num1: rand_num1,
            num2: rand_num2,
        }
    }
}

fn main() {
    let mut rng = rand::thread_rng();
    let rand_custom: MyType = rng.gen();
    println!("Random Point: {:?}", rand_custom);
}

Output:

$random> cargo run
Compiling random v0.1.0 (/home/arvind/Desktop/rust/random)
    Finished dev [unoptimized + debuginfo] target(s) in 0.32s
     Running `target/debug/random`

Random Point: MyType { num1: 24979, num2: -29976 }

Explanation:

In the above program, we imported the "rand" library to our project for generating random numbers. We imported the "rand" library using the below line:

use rand::Rng;

Here, we created a structure MyType with two integer members num1 and num2. To generate values for the custom type we need to implement distribution for that.

In the main() function, we generated values for the custom type using the gen() method and printed the result.

Rust Random Numbers Programs »



ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.