极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9911|回复: 5

贡献一个开源的“真随机数”发生函数

[复制链接]
发表于 2012-9-21 00:12:29 | 显示全部楼层 |阅读模式
本帖最后由 test01 于 2012-11-3 01:46 编辑
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

  4. #include <stdlib.h>

  5. void generating_random_seed (uint_fast16_t seed_width, uint16_t seed_bits[])  //Occupied A7 pin. Arduino with the statement.
  6. {
  7.   const uint_fast16_t MAX_WIDTH = 256;
  8.   const uint_fast8_t NOISE_INPIT_PIN = A7;
  9.   
  10.   if ((seed_width <= 0) and (seed_width > MAX_WIDTH))
  11.   {
  12.     return;
  13.   }

  14.   analogReference(INTERNAL);  //Arduino with the statement
  15.   uint_fast8_t analog_bit_n0, analog_bit_n1;
  16.   uint16_t analog_bits;
  17.   int_fast8_t n = 0;
  18.   analog_bit_n0 = read_bit(analogRead(NOISE_INPIT_PIN), 16, 0);  //Arduino with the statement
  19.   while (n < 16)
  20.   {
  21.     analog_bit_n1 = read_bit(analogRead(NOISE_INPIT_PIN), 16, 0);  //Arduino with the statement
  22.     if (analog_bit_n0 not_eq analog_bit_n1)
  23.     {
  24.       analog_bits = xor_bit(analog_bits, 16, n, (analog_bit_n0 > analog_bit_n1) ? 0 : 1);
  25.       analog_bit_n0 = read_bit(analogRead(NOISE_INPIT_PIN), 16, 0);  //Arduino with the statement
  26.       ++n;
  27.     }
  28.     else
  29.     {
  30.       analog_bit_n0 = analog_bit_n1;
  31.     }
  32.   }
  33.   srand(analog_bits);
  34.   
  35.   const uint16_t WORD_10 = (((uint16_t)B10101010) << 8) | B10101010;
  36.   const uint16_t WORD_01 = compl WORD_10;
  37.   const uint16_t WORD_11 = compl((uint16_t)0);
  38.   div_t array_length = div(seed_width - 1, 16);
  39.   uint16_t initialization_bits;
  40.   ((rand() % 2) == 0) ? initialization_bits = WORD_10 : initialization_bits = WORD_01;
  41.   for (int_fast8_t i = 0; i <= array_length.quot; ++i)
  42.   {
  43.     seed_bits[i] = initialization_bits;
  44.   }
  45.   seed_bits[array_length.quot] and_eq compl(WORD_11 << (array_length.rem + 1));
  46.   
  47.   analog_bit_n0 = read_bit(analogRead(NOISE_INPIT_PIN), 16, 0);  //Arduino with the statement
  48.   for (int_fast16_t i = (int_fast16_t)seed_width * 3 - 1; i >= 0; --i)
  49.   {
  50.     ldiv_t seed_pointer = ldiv((int_fast32_t)seed_width * rand() / ((uint_fast16_t)RAND_MAX + 1), 16);
  51.     analog_bit_n1 = read_bit(analogRead(NOISE_INPIT_PIN), 16, 0);  //Arduino with the statement
  52.     if (analog_bit_n0 not_eq analog_bit_n1)
  53.     {
  54.       seed_bits[seed_pointer.quot] = xor_bit(seed_bits[seed_pointer.quot], 16, seed_pointer.rem, (analog_bit_n0 > analog_bit_n1) ? 0 : 1);
  55.       analog_bit_n0 = read_bit(analogRead(NOISE_INPIT_PIN), 16, 0);  //Arduino with the statement
  56.     }
  57.     else
  58.     {
  59.       analog_bit_n0 = analog_bit_n1;
  60.     }
  61.   }
  62. }
复制代码

===========================
说明:这用了A7模拟输入脚,让A7接一根悬空的长引线效果好
可生成1位到256位的真随机数
注意,受avr里的adc所限,产生的随机数如果要用在质量要求高的应用,一定先行检验是否符合要求
欢迎大家来检验这个发生器的随机性
回复

使用道具 举报

发表于 2012-9-21 16:01:05 | 显示全部楼层
{:soso_e103:},好东西。。。改天抽样测评下。。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-9-22 01:32:40 | 显示全部楼层
修正一个bug,65行里是ldiv_t和ldiv,不是div_t和div
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-10-13 02:47:37 | 显示全部楼层
为更方便大家应用,变更了开源许可证
回复 支持 反对

使用道具 举报

发表于 2012-10-14 08:33:49 | 显示全部楼层
回复 支持 反对

使用道具 举报

 楼主| 发表于 2012-11-3 01:47:04 | 显示全部楼层
晕死
漏写了#include <stdlib.h>
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-28 06:02 , Processed in 0.041076 second(s), 18 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表