Skip to content

Commit

Permalink
feat: add gen_prime generator
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinGimbel committed Apr 19, 2024
1 parent 3e3438a commit 59fa392
Show file tree
Hide file tree
Showing 5 changed files with 1,049 additions and 2 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
- [domain](#domain)
- [http method](#http-method)
- [ipv4](#ipv4)
- [Generators with arguments](#generators-with-arguments)
- [gen_prime](#gen_prime)
- [enum](#enum)
- [int](#int)
- [private ipv4](#private-ipv4)
Expand Down Expand Up @@ -153,6 +153,20 @@ let ip: String = gen_ipv4();
// ip = "168.11.40.75"
```

#### gen_prime

Returns one of the first 1000 prime numners, randomely.

Function signature
```rust
gen_prime() -> usize
```

Example call
```rust
let prime: usize = gen_prime();
// prime = 6323
```
### Generators with arguments
[⬆️ Back to Top](#table-of-contents)

Expand Down
9 changes: 9 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::error::Error;

pub mod corpora;
pub mod tlds;
pub mod primes;

/// JSONDataset represents a generic data structure for storing the parsed JSON. Each JSON taken
/// from Corpora has a `data` field which is an Array of Strings in JSON (= Vec<String> in Rust).
Expand Down Expand Up @@ -79,3 +80,11 @@ pub fn gen_switch(name: String) -> String {
pub fn gen_corpora_switch(name: String) -> String {
return gen_switch(name);

Check warning on line 81 in src/data.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> src/data.rs:81:5 | 81 | return gen_switch(name); | ^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return help: remove `return` | 81 - return gen_switch(name); 81 + gen_switch(name) |
}

// gen_prime returns a random of the first 1000 prime numbers
pub fn gen_prime() -> usize {
let mut rnd = rand::thread_rng();
let index = rnd.gen_range(0..primes::DATA_PRIMES.len() - 1);

primes::DATA_PRIMES[index]
}
2 changes: 1 addition & 1 deletion src/data/corpora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3902,4 +3902,4 @@ pub const DATA_MOOD: &str = r#"
"zealous"
]
}
"#;
"#;
Loading

0 comments on commit 59fa392

Please sign in to comment.