Skip to content

Commit 26bfdaa

Browse files
fix: rand err's
1 parent b37a8f1 commit 26bfdaa

File tree

3 files changed

+11
-9
lines changed

3 files changed

+11
-9
lines changed

src/notation.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl Clause {
187187
///
188188
/// Contains a vector of [`Clause`]s, a vector of [`Literal`]s, a vector of variables, the number of clauses and the number of variables
189189
#[derive(Debug, Clone)]
190+
#[allow(dead_code)]
190191
pub struct Formula {
191192
pub clauses: Vec<Clause>,
192193
pub literals: Vec<Literal>,
@@ -225,7 +226,7 @@ impl Formula {
225226
///
226227
/// # Examples
227228
/// Assuming the CNF file is in `/bin/problem.cnf` and contains the following:
228-
/// ```;
229+
/// ```custom,{class=language-bash}
229230
/// p cnf 3 1
230231
/// 1 -3 0
231232
/// 2 3 -1 0

src/solvers/gsat.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use rand::seq::SliceRandom;
2-
31
use crate::notation::{Clause, Formula};
42
use crate::solvers::utils::flip;
3+
use rand::prelude::IndexedRandom;
4+
use rand::Rng;
55
use std::collections::HashMap;
66

77
/// GSAT Algorithm for evaluation of propostional formulas
@@ -135,11 +135,11 @@ pub fn gsat_algorithm(
135135
interpretation = flip(&mut interpretation, var_to_flip).clone();
136136
} else {
137137
// Randomly select a clause that is not satisfied by the interpretation
138-
let clause = unsatisfied_clauses.choose(&mut rand::thread_rng());
138+
let clause = unsatisfied_clauses.choose(&mut rand::rng());
139139
let clausal_variables: Vec<i32> =
140140
formula.get_clausal_variables(&clause.unwrap());
141141
let random_var =
142-
clausal_variables[rand::random::<usize>() % clausal_variables.len()];
142+
clausal_variables[rand::rng().random_range(0..clausal_variables.len())];
143143
interpretation = flip(&mut interpretation, random_var).clone();
144144
}
145145
}

src/solvers/wsat.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use rand::seq::SliceRandom;
2-
31
use crate::notation::{Clause, Formula};
42
use crate::solvers::utils::flip;
3+
use rand::prelude::IndexedRandom;
4+
use rand::Rng;
55
use std::collections::HashMap;
66

77
/// WSAT Algorithm for evaluation of propostional formulas
@@ -92,11 +92,12 @@ pub fn wsat_algorithm(formula: &mut Formula, max_tries: u32, max_flips: u32) ->
9292
for _ in 0..max_flips {
9393
// Randomly select a clause that is not satisfied by the interpretation
9494
let unsatisfied_clauses: Vec<Clause> = formula.get_unsatisfied_clauses();
95-
let clause = unsatisfied_clauses.choose(&mut rand::thread_rng());
95+
let clause = unsatisfied_clauses.choose(&mut rand::rng());
9696

9797
// Randomly select a variable from the clause
9898
let clausal_variables: Vec<i32> = formula.get_clausal_variables(&clause.unwrap());
99-
let variable = clausal_variables[rand::random::<usize>() % clausal_variables.len()];
99+
let variable =
100+
clausal_variables[rand::rng().random_range(0..clausal_variables.len())];
100101

101102
interpretation = flip(&mut interpretation, variable).clone();
102103

0 commit comments

Comments
 (0)