1、rand() 方法

rand()不需要参数,它会返回一个从0到最大随机数的任意整数,最大随机数的大小通常是固定的一个大整数。 这样,如果你要产生0~10的10个整数,可以表达为:

 
int N = rand() % 11;

这样,N的值就是一个0~10的随机数,如果要产生1~10,则是这样:

 
int N = 1 + rand() % 10;

 总结来说,可以表示为:  

 
a + rand() % n

2、random库使用

#include <iostream>
#include <random>
using std::cout; using std::endl;
using std::default_random_engine;
int main()
{
default_random_engine e;
for (size_t i = ; i < ; ++i) //生成十个随机数
cout << e() << endl;
cout << "Min random:" << e.min() << endl; //输出该随机数引擎序列的范围
cout << "Max random:" << e.max() << endl;
return ;
}

--修改随机种子

#include <iostream>
#include <random> using std::cout; using std::endl;
using std::default_random_engine; int main()
{
default_random_engine e; //或者直接在这里改变种子 e(10)
e.seed(); //设置新的种子
for (size_t i = ; i < ; ++i)
cout << e() << endl;
cout << "Min random:" << e.min() << endl;
cout << "Max random:" << e.max() << endl; return ;
}

参考 :

https://www.cnblogs.com/byhj/p/4149467.html

http://www.cplusplus.com/reference/random/?kw=random

C++ Random 的使用的更多相关文章

  1. Chrome V8引擎系列随笔 (1):Math.Random()函数概览

    先让大家来看一幅图,这幅图是V8引擎4.7版本和4.9版本Math.Random()函数的值的分布图,我可以这么理解 .从下图中,也许你会认为这是个二维码?其实这幅图告诉我们一个道理,第二张图的点的分 ...

  2. Math.random()

    Math.random() 日期时间函数(需要用变量调用):var b = new Date(); //获取当前时间b.getTime() //获取时间戳b.getFullYear() //获取年份b ...

  3. .Net使用system.Security.Cryptography.RNGCryptoServiceProvider类与System.Random类生成随机数

    .Net中我们通常使用Random类生成随机数,在一些场景下,我却发现Random生成的随机数并不可靠,在下面的例子中我们通过循环随机生成10个随机数: ; i < ; i++) { Rando ...

  4. 随机数(random)

    需求 Random rd=new Random(); 需要十以内的随机数  (0---10) System.out.println((int)((rd.nextDouble()*100)/10)); ...

  5. python写红包的原理流程包含random,lambda其中的使用和见简单介绍

    Python写红包的原理流程 首先来说说要用到的知识点,第一个要说的是扩展包random,random模块一般用来生成一个随机数 今天要用到ramdom中unifrom的方法用于生成一个指定范围的随机 ...

  6. [LeetCode] Random Pick Index 随机拾取序列

    Given an array of integers with possible duplicates, randomly output the index of a given target num ...

  7. [LeetCode] Linked List Random Node 链表随机节点

    Given a singly linked list, return a random node's value from the linked list. Each node must have t ...

  8. [LeetCode] Copy List with Random Pointer 拷贝带有随机指针的链表

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. php对应js math.random

    <?php function random($min = 0, $max = 1) {     return $min + mt_rand()/mt_getrandmax()*($max-$mi ...

  10. python常用模块(模块和包的解释,time模块,sys模块,random模块,os模块,json和pickle序列化模块)

    1.1模块 什么是模块: 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文 ...

随机推荐

  1. python 项目启动路径自动添加

    import os import sys base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  #找到当前项目 ...

  2. wpgwhpg

    //f[i][j]就是第is时wpgwhpg的疲劳度是j,那么我们就可以就ta这1s是否休息进行讨论 #include<bits/stdc++.h> using namespace std ...

  3. LOJ #2359. 「NOIP2016」天天爱跑步(倍增+线段树合并)

    题意 LOJ #2359. 「NOIP2016」天天爱跑步 题解 考虑把一个玩家的路径 \((x, y)\) 拆成两条,一条是 \(x\) 到 \(lca\) ( \(x, y\) 最近公共祖先) 的 ...

  4. 【CF526G】Spiders Evil Plan(贪心)

    [CF526G]Spiders Evil Plan(贪心) 题面 洛谷 CodeForces 给定一棵树,要求选择\(y\)条链,满足被链覆盖的所有点在树上联通,且\(x\)必定在联通块中. 对于每次 ...

  5. Squid代理服务部署

    构建Squid代理服务器1.配置IP地址 2.编译安装Squid软件[root@localhost ~]# tar -zxvf squid-3.4.6.tar.gz -C /usr/src/[root ...

  6. IO创建Socket通信中慎用BufferReader中的readLine()

    在编写Socket的Demo的时候,在Server中使用BufferReader获取从客服端发送过来的内容 package cn.lonecloud.socket; import cn.loneclo ...

  7. 51Nod--1117 聪明的木匠(排序)

    我们可以反过来想,如何将这几个线段组成一根 并且每次花费是组成的两段的和 #include<bits/stdc++.h> using namespace std; #define maxn ...

  8. HDU--4825 Xor Sum (字典树)

    题目链接:HDU--4825 Xor Sum mmp sb字典树因为数组开的不够大一直wa 不是报的 re!!! 找了一下午bug 草 把每个数转化成二进制存字典树里面 然后尽量取与x这个位置上不相同 ...

  9. python3安装pycurl

    centos7安装pycurl 出现错误 FileNotFoundError: [Errno 2] No such file or directory: 'curl-config'ImportErro ...

  10. 基于Senparc.CO2NET 缓存策略扩展的缓存使用方法

    没啥说的,直接上代码 1.缓存  CacheFactory 实现: //---------------------------------------------------------------- ...