首页 > 系统相关 >C++ 实现随机数生成(Windows、Linux)

C++ 实现随机数生成(Windows、Linux)

时间:2022-10-16 10:03:59浏览次数:66  
标签:gcc Windows random C++ entropy dev 随机数 Linux main

1、简介

计算机的随机数都是由伪随机数,即是由小M多项式序列生成的,其中产生每个小序列都有一个初始值,即随机种子。(注意: 小M多项式序列的周期是65535,即每次利用一个随机种子生成的随机数的周期是65535,当你取得65535个随机数后它们又重复出现了。)

2、windows随机数

2.1 随机数范围计算公式

产生一定范围随机数的通用表示公式是:

要取得[0,n)  就是rand()%n     表示 从0到n-1的数
要取得[a,b)的随机整数,使用(rand() % (b-a))+ a; 
要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a; 
要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1; 
通用公式:a + rand() % n;其中的a是起始值,n是整数的范围。 
要取得a到b之间的随机整数,另一种表示:a + (int)b * rand() / (RAND_MAX + 1)。 
要取得0~1之间的浮点数,可以使用rand() / double(RAND_MAX)。

2.2 rand()

rand()会返回一随机数值, 范围在0至RAND_MAX 间。RAND_MAX定义在stdlib.h, 其值为2147483647。

#include <iostream>
#include <cstdlib>
using namespace std;

int main()
{
    for (int i = 0; i < 10; i++)
    {
        cout << rand()%100<< " ";
    }
    return 0;
}

2.3 srand()

srand()可用来设置rand()产生随机数时的随机数种子。通过设置不同的种子,我们可以获取不同的随机数序列。可以利用srand((int)(time(NULL))的方法,利用系统时钟,产生不同的随机数种子。不过要调用time(),需要加入头文件< ctime >。

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
    srand((int)time(0));  // 产生随机种子  把0换成NULL也行
    for (int i = 0; i < 10; i++)
    {
        cout << rand()%100<< " ";
    }
    return 0;
}
#include <cstdlib>
#include <ctime>
using namespace std;
int main() 
{
    srand(time(nullptr)); // 用当前时间作为种子
    int min = 5, max = 10;
    int randomValue = (rand() % (max - min)) + min;//范围[min,max)
    randomValue = (rand() % (max - min + 1)) + min;//范围[min,max]
    randomValue = (rand() % (max - min)) + min + 1;//范围(min,max]
}

2.4 c++11 <random>

C++标准建议使用<random>代替它们。 <random>(since C++11) <random>中定义了随机数生成引擎、随机数分布律、不确定随机数和预定义的最佳算法实践。

2.4.1 随机数生成引擎

<random>提供了三种引擎,使用哪种需要权衡:

  • linear_congruential_engine(线性同余随机数引擎):速度比较快,储存很少的中间变量。
  • mersenne_twister_engine(梅森旋转随机数引擎):比较慢,占用存储空间较大,但是在参数设置合理的情况下,可生成最长的不重复序列,且具有良好的频谱特征。
  • subtract_with_carry_engine(带进位随机数引擎):速度最快,占用存储空间较大,频谱特性有时不佳。

上面的三种随机数生成算法均是模板类,需要我们自己进行实例化;模板类实例化需要的参数,均是算法中使用的参数,若是不懂其原理,建议不要使用这些模板类。不过不用担心,在C++11标准中,已经帮我们预先定义了一些随机数类(预定义算法),它们都是用过上面的三个类模板实例化出来的。

2.4.2 随机数分布律

可以预先定义随机数分布的概率分布,如正态分布uniform_int_distribution、伯努利分布binomial_distribution、泊松分布poisson_distribution等等。

2.4.3 预定义算法

定义了算法的最佳实践,避免了参数的选择,可以直接选择引擎,设定分布规律就好。算法包括minstd_rand0、minstd_rand、mt19937、mt19937_64、ranlux24_base、ranlux48_base等。

类名称 属性 依赖类
linear_congruential_engine templates \
mersenne_twister_engine templates \
subtract_with_carry_engine templates \
discard_block_engine Engine adaptors \
independent_bits_engine Engine adaptors \
shuffle_order_engine Engine adaptors \
default_random_engine instantiations 待定
minstd_rand instantiations linear_congruential_engine
minstd_rand0 instantiations linear_congruential_engine
mt19937 instantiations mersenne_twister_engine
mt19937_64 instantiations mersenne_twister_engine
ranlux24_base instantiations subtract_with_carry_engine
ranlux48_base instantiations subtract_with_carry_engine
ranlux24 instantiations ranlux24_base、discard_block_engine
ranlux48 instantiations ranlux48_base、discard_block_engine
knuth_b instantiations shuffle_order_engine

2.4.4 random_device

random_device是标准库提供的一个非确定性随机数生成设备,是所有生成器中唯一一个不需要随机数种子的方式。在Linux中,是需要读取/dev/urandom设备。需要注意的是random_device在某些操作系统中是无法使用的,会在构造函数或者调用operator()函数时抛出异常,因此在进行代码移植时,需要格外注意。

2.4.5 实现代码

#include <random>
using namespace std;

int main(){
  int min = 0,max = 100;
  random_device seed;//硬件生成随机数种子
  ranlux48 engine(seed());//利用种子生成随机数引擎
  uniform_int_distribution<> distrib(min, max);//设置随机数范围,并为均匀分布
  int random = distrib(engine);//随机数
}
#include <iostream>
#include <random>

int main(int argc, char**argv){
	std::default_random_engine engine;
    for (int i = 0; i < 10; ++i ){
        std::cout << engine() << " ";
    }
    std::cout << std::endl;
	return 0;
}

3、linux随机数

3.1 简介

/dev/random 和 /dev/urandom /dev/urandom 是一个伪随机数生成器,缺乏熵它也不会停止。 /dev/random 是一个真随机数生成器,它会在缺乏熵的时候停止。 在这里插入图片描述

3.2 命令

3.2.1 /dev/random

消耗完系统熵池中熵

cat /dev/random
cat /dev/random > /dev/null &

在这里插入图片描述

当熵池中熵值小于阈值(cat /proc/sys/kernel/random/write_wakeup_threshold)时,系统会自动收集熵源数据,添加到熵池,增加熵值;当达到阈值时,系统会停止收集熵源数据,熵池中熵值不会自动继续增加。

3.2.2 /proc/sys/kernel/random

  • 查看接口
ls /proc/sys/kernel/random

在这里插入图片描述

  • 查看系统熵池中拥有的熵数 This read-only file gives the available entropy, in bits. This will be a number in the range 0 to 4096。
cat /proc/sys/kernel/random/entropy_avail

在这里插入图片描述

  • 查看熵池容量 说明熵池的大小(以位为单位)。This file is read-only, and gives the size of the entropy pool in bits. It contains the value 4096
cat /proc/sys/kernel/random/poolsize
  • 查看从熵池中读取熵的阀值 当 entropy_avail 中的值少于这个阀值,这读取 /dev/random 会被阻塞。
cat /proc/sys/kernel/random/read_wakeup_threshold
cat /proc/sys/kernel/random/write_wakeup_threshold
  • 产生随机字符串 These read-only files contain random strings like 6fd5a44b-35f4-4ad4-a9b9-6b9be13e1fe9. The former is generated afresh for each read, the latter was generated once。
cat /proc/sys/kernel/random/uuid
cat /proc/sys/kernel/random/boot_id

在这里插入图片描述

3.2.3 Haveged

Haveged 是一个守护进程,它使用处理器的“抖动”将熵添加到系统熵池中。

  • 安装haveged:
# CentOS 7 yum install rng-tools haveged -y 
# Debian 9 apt install rng-tools haveged -y

在这里插入图片描述

  • 启动haveged并开机启动,并查看haveged状态:
systemctl start haveged                    #启动
systemctl enable haveged                   #开机启动
systemctl restart haveged                  #重新启动
systemctl status haveged                   #查看启动状态
systemctl list-unit-files | grep haveged    #查看开机启动状态

在这里插入图片描述 在这里插入图片描述

#rng-tools
systemctl enable rng-tools 
systemctl status rng-tools
#systemctl start rng-tools
service rngd start

在这里插入图片描述

sudo apt install pv
pv /dev/random > /dev/null 

在这里插入图片描述

使用 pv 我们可以看到我们通过管道传递了多少数据。正如你所看到的,在运行 haveged 之前,我们是 2.1 位/秒(B/s)。而在开始运行 haveged 之后,加入处理器的抖动到我们的熵池中,我们得到大约 1.5MiB/秒。 在这里插入图片描述

3.2.4 cpuinfo

cat /proc/cpuinfo | grep -i processor

在这里插入图片描述

3.2.5 upower

upower 命令预装在大多数的 Linux 发行版本中。为了使用 upower 命令来展示电池的状态,打开终端并运行如下命令:

man upower
upower -e
upower -i /org/freedesktop/UPower/devices/battery_BAT0
upower -i `upower -e | grep 'BAT'`
upower -i $(upower -e | grep BAT) | grep --color=never -E "state|to\ full|to\ empty|percentage"

在这里插入图片描述 在这里插入图片描述

3.2.6 acpi

acpi 命令可以用来显示你的 Linux 发行版本中电池的状态以及其他 ACPI 信息。 在某些 Linux 发行版本中,你可能需要安装 acpi 命令。

## Debian、 Ubuntu 及其衍生版本中安装它
sudo apt-get install acpi
## RHEL、 CentOS、 Fedora 等系统中使用
sudo yum install acpi
sudo dnf install acpi
## 在 Arch Linux 及其衍生版本中使用
sudo pacman -S acpi
man acpi
acpi -V
acpi -a

在这里插入图片描述 在这里插入图片描述

cat /sys/class/power_supply/BAT0/uevent 
cat /sys/class/power_supply/BAT0/capacity
find /sys/class/power_supply/BAT0/ -type f | xargs -tn1 cat

3.2.7 ps

ps

在这里插入图片描述

## ps命令查看所有进程
ps -aux 

在这里插入图片描述

## ps命令查看具体某一应用的所有进程
## 查看chrome 的所有进程
ps -aux|grep chrome

在这里插入图片描述

## top命令当前时刻系统正在运行的所有进程
top

在这里插入图片描述

3.2.8 kill

kill (pid号)

在这里插入图片描述

killall   (program应用名称) 
pkill   (program应用名称) 

## 针对奔溃的窗口进程,无法退出、关闭,无法通过kill进程来终止
xkill

3.3 函数

https://www.zx2c4.com/projects/linux-rng-5.17-5.18/ https://git.kernel.org/pub/scm/linux/kernel/git/crng/random.git/tree/drivers/char/random.c 在这里插入图片描述

3.3.1 ioctl

The following ioctl(2) requests are defined on file descriptors connected to either /dev/random or /dev/urandom. All requests performed will interact with the input entropy pool impacting both /dev/random and /dev/urandom. The CAP_SYS_ADMIN capability is required for all requests except RNDGETENTCNT.

The character special files /dev/random and /dev/urandom (present since Linux 1.3.30) provide an interface to the kernel's random number generator. The file /dev/random has major device number 1 and minor device number 8. The file /dev/urandom has major device number 1 and minor device number 9.

#include <linux/random.h>
int ioctl(fd, RNDrequest, param);
   RNDGETENTCNT
          Retrieve the entropy count of the input pool, the contents
          will be the same as the entropy_avail file under proc.
          The result will be stored in the int pointed to by the
          argument.

   RNDADDTOENTCNT
          Increment or decrement the entropy count of the input pool
          by the value pointed to by the argument.

   RNDGETPOOL
          Removed in Linux 2.6.9.

   RNDADDENTROPY
          Add some additional entropy to the input pool,
          incrementing the entropy count.  This differs from writing
          to /dev/random or /dev/urandom, which only adds some data
          but does not increment the entropy count.  The following
          structure is used:

              struct rand_pool_info {
                  int    entropy_count;
                  int    buf_size;
                  __u32  buf[0];
              };

          Here entropy_count is the value added to (or subtracted
          from) the entropy count, and buf is the buffer of size
          buf_size which gets added to the entropy pool.

   RNDZAPENTCNT, RNDCLEARPOOL
          Zero the entropy count of all pools and add some system
          data (such as wall clock) to the pools.

3.3.2 /proc

The files in the directory /proc/sys/kernel/random (present since 2.3.16) provide additional information about the /dev/random device:

entropy_avail
          This read-only file gives the available entropy, in bits.
          This will be a number in the range 0 to 4096.

   poolsize
          This file gives the size of the entropy pool.  The
          semantics of this file vary across kernel versions:

          Linux 2.4:
                 This file gives the size of the entropy pool in
                 bytes.  Normally, this file will have the value
                 512, but it is writable, and can be changed to any
                 value for which an algorithm is available.  The
                 choices are 32, 64, 128, 256, 512, 1024, or 2048.

          Linux 2.6 and later:
                 This file is read-only, and gives the size of the
                 entropy pool in bits.  It contains the value 4096.

   read_wakeup_threshold
          This file contains the number of bits of entropy required
          for waking up processes that sleep waiting for entropy
          from /dev/random.  The default is 64.

   write_wakeup_threshold
          This file contains the number of bits of entropy below
          which we wake up processes that do a select(2) or poll(2)
          for write access to /dev/random.  These values can be
          changed by writing to the files.

   uuid and boot_id
          These read-only files contain random strings like
          6fd5a44b-35f4-4ad4-a9b9-6b9be13e1fe9.  The former is
          generated afresh for each read, the latter was generated
          once.

3.4 linux代码示例

3.4.1 gcc编译

<font color=grey>那么,到底如何分步编译 C、C++ 程序呢?事实上,GCC 编译器除了提供 gcc 和 g++ 这 2 个指令之外,还提供有大量的指令选项,方便用户根据自己的需求自定义编译方式。在前面的学习过程中,我们已经使用了一些指令选项,比如编译 C++ 程序时 gcc 指令后跟的 -xc++、-lstdc++、-shared-libgcc,再比如手动指定可执行文件名称的 -o 选项。

<font color=green>注意,虽然我们仅编写了一条 gcc 或者 g++ 指令,但其底层依据是按照预处理、编译、汇编、链接的过程将 C 、C++ 程序转变为可执行程序的。而本应在预处理阶段、编译阶段、汇编阶段生成的中间文件,此执行方式默认是不会生成的,只会生成最终的 a.out 可执行文件(除非为 gcc 或者 g++ 额外添加 -save-temps 选项)。

gcc/g++指令选项 功 能
-E(大写) 预处理指定的源文件,不进行编译。
-S(大写) 编译指定的源文件,但是不进行汇编。
-c 编译、汇编指定的源文件,但是不进行链接。
-o 指定生成文件的文件名。
-llibrary(-I library) 其中 library 表示要搜索的库文件的名称。该选项用于手动指定链接环节中程序可以调用的库文件。建议 -l 和库文件名之间不使用空格,比如 -lstdc++。
-ansi 对于 C 语言程序来说,其等价于 -std=c90;对于 C++ 程序来说,其等价于 -std=c++98。
-std= 手动指令编程语言所遵循的标准,例如 c89、c90、c++98、c++11 等。

<font color=blue>当然,gcc 指令也为用户提供了“手动指定代表编译方式”的接口,即使用 -x 选项。例如,gcc -xc xxx 表示以编译 C 语言代码的方式编译 xxx 文件;而 gcc -xc++ xxx 则表示以编译 C++ 代码的方式编译 xxx 文件。

## 如果想使用 gcc 指令来编译执行 C++ 程序,需要在使用 gcc 指令时,手动为其添加 -lstdc++ -shared-libgcc 选项,表示 gcc 在编译 C++ 程序时可以链接必要的 C++ 标准库。
## gcc -xc++ demo.cpp -lstdc++ -shared-libgcc
g++ demo.cpp  #或者 gcc -xc++ -lstdc++ -shared-libgcc demo.cpp
./a.out

## 将 main.c 和 func.c 两个源文件编译成一个可执行文件,其名字为 app.out。
## 如果不使用 -o 选项,那么将生成名字为 a.out 的可执行文件。
gcc main.c func.c -o app.out

## GCC一次编译多文件项目
gcc myfun.c main.c -o main.exe
./main.exe
#or
gcc -c myfun.c main.c
gcc myfun.o main.o -o main.exe
./main.exe
#or
gcc *.c -o main.exe
./main.exe

## GCC 编译器无法找到 cos() 这个函数。为了编译这个 main.c,必须使用-l选项,以链接数学库:
## 数学库的文件名是 libm.a。前缀lib和后缀.a是标准的,m是基本名称,GCC 会在-l选项后紧跟着的基本名称的基础上自动添加这些前缀、后缀,本例中,基本名称为 m。
## 在支持动态链接的系统上,GCC 自动使用在 Darwin 上的共享链接库 libm.so 或 libm.dylib。
gcc main.c -o main.out -lm

## (1)把链接库作为一般的目标文件,为 GCC 指定该链接库的完整路径与文件名。
## 如果链接库名为 libm.a,并且位于 /usr/lib 目录,那么下面的命令会让 GCC 编译 main.c,然后将 libm.a 链接到 main.o:
gcc main.c -o main.out /usr/lib/libm.a

## (2)使用-L选项,为 GCC 增加另一个搜索链接库的目录:
## 可以使用多个-L选项,或者在一个-L选项内使用冒号分割的路径列表。
gcc main.c -o main.out -L/usr/lib -lm

## (3)把包括所需链接库的目录加到环境变量 LIBRARYPATH 中。
  • -Wall选项 使gcc产生尽可能多的警告信息,警告信息很有可能是错误的来源,特别是隐式编程错误,所以尽量保持0 warning。

  • -Werror 选项 要求gcc将所有的警告当作错误进行处理。

  • -fPIC选项 PIC指Position Independent Code。共享库要求有此选项,以便实现动态连接(dynamic linking)。

  • -I 选项(大写的 i) 向头文件搜索目录中添加新的目录。 1、用#include"file"的时候,gcc/g++会先在当前目录查找你所制定的头文件,如 果没有找到,他回到缺省的头文件目录找。 如果使用-I制定了目录,他会先在你所制定的目录查找,然后再按常规的顺序去找. 2、用#include<file>,gcc/g++会到-I制定的目录查找,查找不到,然后将到系统的缺 省的头文件目录查找 例如:gcc –I /usr/dev/mysql/include test.c –o test.o

  • -l选项(小写的 l) 说明库文件的名字。如果库文件为 libtest.so, 则选项为: -ltest

  • -L选项 说明库文件所在的路径。 例如:-L.(“.”表示当前路径)。 -L/usr/lib (“/usr/lib” 为路径。注:这里的路径是绝对路径) 如果没有提供 -L选项,gcc 将在默认库文件路径下搜索

  • -shared选项指定生成动态连接库,不用该标志外部程序无法连接。相当于一个可执行文件, 生成 .so 文件

  • -static 选项,强制使用静态链接库,生成 .a 文件。因为gcc在链接时优先选择动态链接库,只有当动态链接库不存在时才使用静态链接库。加上该选项可强制使用静态链接库。 .so 和 .a 的区别:运行时动态加载,编译时静态加载

3.4.2 g++编译

g++ filename.cpp
g++ filename.cpp -o filename

g++ -c 1.cpp -o 1.o
g++ -c 2.cpp -o 2.o
g++ 1.o 2.o -o out

g++ -o filename filename.cpp
g++ -o filename file1.cpp file2.cpp

3.4.3 std::random_device

#include <iostream>
#include <random>
double getRandomDevice()
{
	std::random_device rd;
	std::mt19937 mt(rd());
	return (unsigned int)mt();
}

在这里插入图片描述

结语

如果您觉得该方法或代码有一点点用处,可以给作者点个赞,或打赏杯咖啡;╮( ̄▽ ̄)╭ 如果您感觉方法或代码不咋地//(ㄒoㄒ)//,就在评论处留言,作者继续改进;o_O??? 如果您需要相关功能的代码定制化开发,可以留言私信作者;(✿◡‿◡) 感谢各位大佬童鞋们的支持!( ´ ▽´ )ノ ( ´ ▽´)っ!!!

标签:gcc,Windows,random,C++,entropy,dev,随机数,Linux,main
From: https://blog.51cto.com/u_15800063/5760144

相关文章

  • Kali-linux快速 搭建vulfocus靶场
    更新kalilinuxsudoaptupdate&&sudoaptupgrade-ysudoaptdist-upgradeapt-getinstalldocker*servicedockerstartdocker-compose下载安装​​https://github.co......
  • 第4章 C++ STL无序关联式容器总结
    除了序列式容器和关联式容器之外,C++11标准库又引入了一类容器,即无序关联式容器。 无序关联式容器,又称哈希容器。和关联式容器一样,此类容器存储的也是键值对元素;不同之......
  • linux文件系统与日志分析
    一、inode结构1.1inode与block(1)文件的概述文件数据包括元信息与实际信息;文件存储在硬盘上,硬盘最小存储单位是“扇区”,每个扇区存储512字节。一个文件必须占用一个inode,......
  • 内网KMS激活Windows OPENWRT
    电脑端配置首先,保证你的WINDOWS系统和OFFICE是VOL版的,这样才可以激活。WINDOWS系统除了旗舰版和家庭版都能激活。(我使用WIN10专业版)OFFICE2016在MSDN只有专业增强版,下......
  • Linux命令
    命令查询man-kxxx查询各种命令。版本uname-a内核版本号cat/etc/issueUbuntu版本号sudolsb_release-aUbuntu发行版系统procinfo显示系统状态/proc/sys动态修......
  • linux安装并配置svn
    1、通过yum命令安装svnserviceyum-yinstallsubversion2、创建svn版本库目录,命令如下mkdir-p/usr/local/svn3、创建svn版本库svnadmincreat......
  • Linux 文件系统与日志分析
    导图1.inode表结构每个文件的属性信息,比如:文件的大小,时间,类型,权限等,称为文件的元数据(metadata)元数据是存放在inode(indexnode)表中。inode表中有很多条记录组成,第一......
  • Sentinel安装教程【Linux+windows】
    一、Sentinel的简介Sentinel是阿里巴巴出品的一款流控组件,它以流量为切入点,在流量控制、断路、负载保护等多个领域开展工作,保障服务可靠性。如果你学过netflix公司旗下......
  • Linux的日志文件简介
    一、日志文件1.1、日志文件介绍日志文件是用于记录系统操作事件的记录文件或文件集合,操作系统有操作系统日志文件,数据库系统有数据库系统日志文件,等等。日志文件内容包......
  • Linux系统安全和应用
    一、账号的安全控制1.1、账号的优化账号的优化一般分为下面几个步骤将一些用户设置无法登录,防止其破坏系统锁定无用账号删除长期不登录的闲置账号锁定用户和账号密......