kfr

Fast, modern C++ DSP framework, FFT, Sample Rate Conversion, FIR/IIR/Biquad Filters (SSE, AVX, AVX-512, ARM NEON)
Log | Files | Refs | README

random.cpp (3893B)


      1 /**
      2  * KFR (https://www.kfrlib.com)
      3  * Copyright (C) 2016-2023 Dan Cazarin
      4  * See LICENSE.txt for details
      5  */
      6 
      7 #include <kfr/base/random.hpp>
      8 #include <kfr/base/reduce.hpp>
      9 #include <kfr/io/tostring.hpp>
     10 
     11 namespace kfr
     12 {
     13 inline namespace CMT_ARCH_NAME
     14 {
     15 
     16 template <typename T, size_t N>
     17 static void test_random(random_state& state, const vec<T, N>& value)
     18 {
     19     const vec<T, N> r = kfr::random_uniform<T, N>(state);
     20     CHECK(r == value);
     21 }
     22 
     23 TEST(random_bit_generator)
     24 {
     25     random_state gen = random_init(1, 2, 3, 4);
     26     test_random(gen, pack<u8>(21, 62, 88, 30, 46, 234, 205, 29, 41, 190, 212, 81, 217, 135, 218, 227));
     27     test_random(gen, pack<u16>(48589, 33814, 55928, 14799, 26904, 18521, 20808, 50888));
     28     test_random(gen, pack<u32>(1554764222, 1538765785, 2072590063, 2837641155));
     29     test_random(gen, pack<u64>(4036091275566340174, 2928916964115561767));
     30 
     31     test_random(gen, pack<i8>(-96, -23, -113, -39, 77, -9, 0, -32, 39, -52, 41, 78, 67, 89, 8, -78));
     32     test_random(gen, pack<i16>(31457, -7338, 25953, -9110, 19500, 20130, 918, -22379));
     33     test_random(gen, pack<i32>(546576509, -1598068238, 1068387634, 521513578));
     34     test_random(gen, pack<i64>(-1351534496328422696, -2056493566767233880));
     35 
     36     test_random(gen, pack<u8>(184));
     37     test_random(gen, pack<u16>(3766));
     38     test_random(gen, pack<u32>(2707944294));
     39     test_random(gen, pack<u64>(7319132094569945994));
     40 
     41     test_random(gen, pack<i8>(-87));
     42     test_random(gen, pack<i16>(30605));
     43     test_random(gen, pack<i32>(481763738));
     44     test_random(gen, pack<i64>(-4633152737592448342));
     45 
     46     test_random(
     47         gen, pack<f32>(0.68433606624603271, 0.33949351310729980, 0.99597716331481934, 0.71035039424896240));
     48     test_random(gen, pack<f64>(0.84738329802487988, 0.30307218960751059));
     49     test_random(gen, pack<f32>(0.35931551456451416));
     50     test_random(gen, pack<f64>(0.95290433236856908));
     51 
     52     test_random(gen, pack<u8>(218, 34, 127));
     53     test_random(gen, pack<u64>(9862453404643991575ull, 6719261899771853693, 7583499760963945490,
     54                                504102580557590315, 3864622132344054582));
     55 
     56     test_random(gen,
     57                 pack<f32>(0.48961830139160156, 0.29450380802154541, 0.75503039360046387, 0.63871228694915771,
     58                           0.76648020744323730, 0.54290330410003662, 0.77374207973480225, 0.91389560699462891,
     59                           0.55802989006042480, 0.81261849403381348));
     60     test_random(gen,
     61                 pack<f64>(0.87351817405232857, 0.07188206926267671, 0.45094433025385028, 0.11828513023601239,
     62                           0.48852715595764762, 0.73555664715112745, 0.60336462206956543, 0.70802907880871735,
     63                           0.66104424809495010, 0.65705152810593415, 0.94064561507444644, 0.33550309924374822,
     64                           0.80028288039450723));
     65 }
     66 
     67 TEST(gen_random_range)
     68 {
     69     random_state gen         = random_init(1, 2, 3, 4);
     70     univector<fbase, 1000> v = gen_random_range<fbase>(std::ref(gen), -1.0, 1.0);
     71     CHECK(minof(v) >= fbase(-1.0));
     72     CHECK(maxof(v) <= fbase(1.0));
     73     // println(mean(v));
     74 }
     75 
     76 TEST(random_normal)
     77 {
     78     random_state gen = random_init(1, 2, 3, 4);
     79     vec<fbase, 12> r = random_normal<12, fbase>(gen, 0.0, 1.0);
     80     println(r);
     81     r                 = random_normal<12, fbase>(gen, 0.0, 1.0);
     82     vec<fbase, 11> r2 = random_normal<11, fbase>(gen, 0.0, 1.0);
     83     println(r2);
     84 
     85     expression_histogram h = histogram_expression<20>(gen_random_normal<double>(std::ref(gen)) * 0.15 + 0.5);
     86     sink(truncate(h, 1000));
     87     println(h.data.below());
     88     println(h.data.values());
     89     println(h.data.above());
     90     sink(truncate(h, 10000));
     91     println(h.data.below());
     92     println(h.data.values());
     93     println(h.data.above());
     94     auto hh = histogram(truncate(h, 100000), 20);
     95     println(hh.below());
     96     println(hh.values());
     97     println(hh.above());
     98 }
     99 } // namespace CMT_ARCH_NAME
    100 } // namespace kfr