got rid of redundant sanity test

This commit is contained in:
randogoth 2025-01-17 13:48:12 +02:00
parent a8c74caf90
commit f3766124ad
3 changed files with 6 additions and 44 deletions

View file

@ -36,9 +36,9 @@ fn main() -> io::Result<()> {
} }
println!("\nTesting {} bytes from {}.", input_data.len(), source); println!("\nTesting {} bytes from {}.", input_data.len(), source);
println!("-----------------------------------------------------"); println!("--------------------------------------------------------");
println!("Randomness Test Observation Z-Score P-Value Pass"); println!("Randomness Test Value Z-Score P-Value Pass");
println!("-----------------------------------------------------"); println!("--------------------------------------------------------");
let mut passed_tests = 0; let mut passed_tests = 0;
let alpha = 0.01; let alpha = 0.01;
@ -49,7 +49,6 @@ fn main() -> io::Result<()> {
// Run each test with the appropriate handling // Run each test with the appropriate handling
let tests = [ let tests = [
("Shannon", Onod::shannon(&input_data)), ("Shannon", Onod::shannon(&input_data)),
("Sanity", Onod::sanity(&input_data)),
("Monobit", Onod::monobit(&input_data)), ("Monobit", Onod::monobit(&input_data)),
("ChiBit", Onod::chi_bit(&input_data)), ("ChiBit", Onod::chi_bit(&input_data)),
("ChiByte", Onod::chi_byte(&input_data)), ("ChiByte", Onod::chi_byte(&input_data)),
@ -79,7 +78,7 @@ fn main() -> io::Result<()> {
}; };
println!( println!(
"{:<15} {:>15.3} {:>8.4} {:.4} {}", "{:<15} {:>15.3} {:>8.4} {:>7.4} {:>2}",
test_name, observation, z_score, p_value, result test_name, observation, z_score, p_value, result
); );
@ -88,9 +87,9 @@ fn main() -> io::Result<()> {
p_values.push(*p_value); p_values.push(*p_value);
} }
println!("-----------------------------------------------------"); println!("--------------------------------------------------------");
println!("{}/{} tests passed.", passed_tests, tests.len()); println!("{}/{} tests passed.", passed_tests, tests.len());
println!("-----------------------------------------------------"); println!("--------------------------------------------------------");
Ok(()) Ok(())
} }

View file

@ -10,7 +10,6 @@ pub mod pi;
pub mod prediction; pub mod prediction;
pub mod runs; pub mod runs;
pub mod runups; pub mod runups;
pub mod sanity;
pub mod shannon; pub mod shannon;
pub mod shells; pub mod shells;
pub mod uncorrelation; pub mod uncorrelation;

View file

@ -1,36 +0,0 @@
use crate::Onod;
impl Onod {
/// Sanity randomness test
/// Checks for basic properties of randomness and returns a p-value.
pub fn sanity(samples: &[u8]) -> (f64, f64, f64) {
if samples.is_empty() {
return (-1.0, 0.0, 1.0);
}
// Check the proportion of ones and zeros in the byte data
let mut one_bits = 0;
let mut total_bits = 0;
for &byte in samples {
one_bits += byte.count_ones();
total_bits += 8;
}
let observed_ratio = one_bits as f64 / total_bits as f64;
let expected_ratio = 0.5; // Expected for a truly random sequence
let std_dev = (0.5 * 0.5 / total_bits as f64).sqrt(); // Standard deviation for binomial distribution
// Z-score calculation
let z_score = (observed_ratio - expected_ratio) / std_dev;
// Use normal distribution to calculate p-value
use statrs::distribution::{Normal, ContinuousCDF};
let normal_dist = Normal::new(expected_ratio, std_dev).expect("Failed to create Normal distribution");
let p_value = 1.0 - normal_dist.cdf(observed_ratio);
(observed_ratio, z_score, p_value)
}
}