In this program, we will generate 8-bit, 16-bit, 32-bit floating-point numbers using the gen() method and print the result.
Add random external library to your project
- Create your project using the below command.
 $cargo new random --bin 
- Goto the project folder cd random and edit Cargo.toml file.
 $random>nano Cargo.toml 
- Then add dependency in Cargo.toml file
 [dependencies]
rand = "0.5.5" 
- After that, build your project using the below command
 $random>cargo build 
- Then execute your project after modification in the src/main.rs source file.
 $random>cargo run 
 
                            
Program/Source Code:
The source code to generate floating-point random numbers is given below. The given program is compiled and executed on UBUNTU 18.04 successfully.
Output:
$random> cargo run Finished dev [unoptimized + debuginfo] target(s) in 0.00s Running `target/debug/random` Random f32: 0.9855966 Random f64: 0.5490820251778854Explanation:
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:
In the main() function, we generated 8-bit, 16-bit, 32-bit floating-point numbers using the gen() method and printed the result.
need an explanation for this answer? contact us directly to get an explanation for this answer