随机数生成

随机数程序生成

依据 


srand48和drand48的原理实现


 

头文件编写

#pragma once
#include<stdlib.h>

#define a 0x5DEECE66DLL
#define c 0xB16
#define m 0x100000000LL

static unsigned long long seed = 1;

double drand48(void) {
	seed = (a*seed + c) & 0xFFFFFFFFFFFFLL;
	unsigned int x = seed >> 16;
	return ((double)x / (double)m);
}

void srand48(unsigned int i) {
	seed = (((long long int)i) << 16) | rand();

}

 源文件调用

#include <stdlib.h>
#include<stdio.h>
#include <iostream>
#include"random.h"
using namespace std;

int main() {
	srand48(12);
	int numSims = 10;
	int count = 10;
	float rand = drand48();
	while (count) {
		rand = drand48();
		cout << rand << endl;
		count--;
	}
	return 0;
}

结果生成

在C / C ++中,drand48()函数已知是具有均匀分布的随机生成器(获得0到1之间的任何值的概率相等)。

概率分布图如图所示:

图源来自:http://www.scratchapixel.com/lessons/mathematics-physics-for-computer-graphics/monte-carlo-methods-mathematical-foundations/inverse-transform-sampling-method

随机数发生器 参考链接https://blog.csdn.net/jimmyblind/article/details/5550042

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值