伪随机算法c语言,伪随机算法实现各语言实现示例。

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

无聊,又研究了一下几种排序算法,在测速的时候,发现自己忘记了一个重要的问题,在某天看到有人在帖吧提到生成随机数只计数到32768就停止了,顺手查了一下C库函数的实现,才发现提供的只是0~32767之间的随机数,我去,图省力,直接用标准库去测试算法,实在是,以前自己弄过随机数C++类啊,居然没有拿来用,失算。重新查了一下一些随机数算法,挑了三个流行的常用算法,重新实现伪随机数生成算法库。通过msvc和mingw编译。

C示例:

//random.h

#ifndef __RANDOM_HEADER__

#define __RANDOM_HEADER__

#include

#ifdef _MSC_VER

#if _MSC_VER < 1600

typedef unsigned int uint32_t;

typedef unsigned char uint8_t;

typedef unsigned __int64 uint64_t;

typedef __int64 int64_t;

#else

#include

#endif

#else

#include

#endif

/**

*三种随机数算法函数库,支持long long随机数生成

*/

struct tagRand

{

void (*seed)(void);// 设置算法种子

int (*next)(void);// 计算得到一个伪随机数

int (*range)(int low, int hight);//计算[low, high]之间的伪随机数

short (*next_short)(void);//返回0-32767之间的伪随机数

double (*next_double)(void);//随机浮点数

float (*next_float)(void);//随机浮点数

int64_t (*next64)(void);// 64位随机数,正负问题自行处理

void (*set_type)(int type); // 设置使用哪种随机算法type=0~2(lcm, xorshift32, well算法)

};

extern struct tagRand Rand;

#endif

//random.c

#include "random.h"

static uint32_t seed[16];

static uint64_t seed64;

static uint32_t get_seed(){ return (unsigned)time(0); }

// C数据结构一书中线性同余法

static int lcm()

{

//A(48271), M(2147483647), Q=M/A(44488), R=M%A(3399)

//公式:A*(seed % Q) - R*(seed/Q) 不使用宏,避免宏替换影响int A[]这样的参数

const int A = 48271, Q=44488, R = 3399;

*seed = A * (*seed % Q) - R * (*seed / Q);

return *seed;

}

// xorshift 32位算法, 另一种写法,周期较长

static int xor128()

{

uint32_t t=*seed^(*seed<<11);

*seed=seed[1]; seed[1]=seed[2]; seed[2]=seed[3];

return seed[3]=seed[3]^(seed[3]>>19)^t^(t>>8) ;

}

// xorshift 32位算法

static int xorshift32()

{

uint32_t x = *seed;

x ^= x << 13;

x ^= x >> 17;

x ^= x << 5;

*seed = x;

return x;

}

// well算法

static int well512()

{

uint32_t a,b,c,d;

static int index = 0;

a = seed[index];

c = seed[(index+13)&15];

b = a^c^(a<<16)^(c<<15);

c = seed[(index+9)&15];

c ^= (c>>11);

a = seed[index] = b^c;

d = a ^((a<<5)& 0xDA442D20);

index = (index + 15)&15;

a = seed[index];

seed[index] = a^b^d^(a<<2)^(b<<18)^(c<<28);

return seed[index];

}

// 默认使用算法

static int (*rands)() = xorshift32;

//设置使用哪种随机算法

static void set_algorithm(int type)

{

switch(type)

{

case 0: rands = lcm;

case 1: rands = xorshift32;

case 2: rands = well512;

}

}

// 计算得到一个伪随机数

static int next(void)

{

return rands() & 0x7fffffff;

}

//计算[low, high]之间的伪随机数

static int range_next(int low, int high)

{

return low + next() % (high+1-low);

}

//返回0-32767之间的伪随机数

static short next_short(void)

{

return next() >> 16;

}

//随机浮点数

static double next_double(void)

{

return (double)next() / 0x7fffffff;

}

//随机浮点数

static float next_float(void)

{

return (float)next() / 0x7fffffff;

}

// 64位随机数,正负问题自行处理

static int64_t next64(void)

{

uint64_t x = seed64;

x ^= x >> 12;

x ^= x << 25;

x ^= x >> 27;

seed64 = x;

return x * 0x2545F4914F6CDD1D;

}

// 设置算法种子

static void seed_rand(void)

{

int i;

for( i = 0; i <8; ++i){

seed[i] = (get_seed() << (i+1))-1;

seed[i+1] = get_seed()>>(i+1);

}

seed64 = *seed;

seed64 = (seed64 << 32) | seed[next() & 15];

}

// 初始化结构体

struct tagRand Rand = {

seed_rand, next, range_next,

next_short, next_double, next_float, next64,

set_algorithm

};

//测试程序:

#include

#include "random.h"

int main()

{

int i, size = 15;

Rand.set_type(2);

Rand.seed();

//随机整数测试

for(i = 0; i < size; ++i)

printf("%d ", Rand.next()%10);

putchar('\n');

//范围测试

for(i = 0; i < size; ++i)

printf("%c ", Rand.range('A', 'Z'));

putchar('\n');

for(i = 0; i < size; ++i)

printf("%c ", Rand.range('a', 'z'));

putchar('\n');

//浮点数测试

for(i = 0 ; i < 10; ++i)

printf("%f\n", Rand.next_float());

//64位数测试

for(i = 0; i < 3; ++i)

printf("%I64d\n", Rand.next64());

//for(i=0; i < size; ++i)

//printf("%c ", rand_char());

//putchar('\n');

printf("sizeof rand:%d\n", sizeof(Rand));

}

运行结果:

0 7 1 7 0 1 9 2 5 6 2 3 1 2 9

E A P S W U T S V B S U Z W X

t f u n d c m s c z e y r f s

0.389461

0.888160

0.725454

0.885750

0.817395

0.770790

0.688366

0.003831

0.185819

0.965431

135809110265936984

-2632048472102704751

6521441842461015894

sizeof rand:32

请按任意键继续. . .

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值