怎样提高随机数的精度?

beepbug 2004-11-17 01:42:26
有一程序要用到随机数,且要求较高。
...全文
230 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
grooving 2004-11-18
  • 打赏
  • 举报
回复
给你一个我也看不懂的。你研究一下。

matrix.c

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

#define MAT_SIZE (16)

#define DEBUG

int init_random();
int **init_mat(int);
int mat_mul(int **, int **, int **, int);
int mat_print(int **A, int n);

int main(int argc, char **argv)
{
int **A,**B,**C;
int n;

/* check the command line argument */

if (argc > 2)
{
printf("Error, usage: %s [matrix size].\n",argv[0]);
return -1;
}
else if (argc == 2)
{
if ((n=atoi(argv[1])) < 1)
{
printf("Error, the argument must be a positive integer.\n");
return -2;
}
}
else n=MAT_SIZE;
init_random(); /* initialize the random */
if ((A=init_mat(n)) == (int**)-1) exit(-1); /* initialize matrix A */
if ((B=init_mat(n)) == (int**)-1) exit(-1); /* initialize matrix B */
if ((C=init_mat(n)) == (int**)-1) exit(-1); /* initialize matrix C */
if (mat_mul(A,B,C,n) == -1) exit(-2); /* multiply A and B into C */

#ifdef DEBUG
printf("A:\n");
mat_print(A,n);
printf("B:\n");
mat_print(B,n);
printf("C:\n");
mat_print(C,n);
#endif

exit(0);
};

/*
init_random:
This function initializes the pseudo-random stream used with the random
function. These are standard unix supplied values.
In order to assure a new random set every time, I used the system time
as a seed to initialize the pseudo random generator.
*/

int init_random()
{
static long state1[32] = {
3,
0x9a319039, 0x32d9c024, 0x9b663182, 0x5da1f342,
0x7449e56b, 0xbeb1dbb0, 0xab5c5918, 0x946554fd,
0x8c2e680f, 0xeb3d799f, 0xb11ee0b7, 0x2d436b86,
0xda672e2a, 0x1588ca88, 0xe369735d, 0x904f35f7,
0xd7158fd6, 0x6fa6f051, 0x616e6b96, 0xac94efdc,
0xde3b81e0, 0xdf0a6fb5, 0xf103bc02, 0x48f340fb,
0x36413f93, 0xc622c298, 0xf5a42ab8, 0x8a88d77b,
0xf5ad9d0e, 0x8999220b, 0x27fb47b9
};
unsigned seed = time(NULL); /* time based random seed */
int n = 128; /* must use the state size */
initstate(seed, (char*)state1, n); /* initialize the state for random */
setstate((char*)state1);
return 0;
};

/*
init_mat:
This function input is an integer - the row/col size of the new matrix.
It allocates memory for the new matrix and returns a pointer to it.
If it fails it returns -1.
The newly allocated matrix is filled with random values.
*/

int **init_mat(int n)
{
int i,j;
int **ptr = malloc(n*sizeof(int*)); /* allocate rows */
if (!ptr) return (int**)-1;
for (i=0 ; i<n ; i++)
{
ptr[i] = malloc(sizeof(int)*n); /* allocate columns */
if (!ptr) return (int**)-1;
};
for (i=0 ; i<n ; i++)
for (j=0 ; j<n ; j++)
ptr[i][j]=random()&100; /* fill it with numbers from 0 to 100 */
return (int**) ptr;
};

/*
mat_mul:
Inputs: pointers to three matrices and int N, all are assumed to be N*N.
Output: -1 for failure, else 0 and the third matrix C is set to A*B.
*/

int mat_mul(int **A, int **B, int **C, int n)
{
int i,j,k;
if (!A || !B || !C || n<1) return -1;
for (i=0 ; i<n ; i++)
{
for (j=0 ; j<n ; j++)
{
C[i][j]=0;
for (k=0 ; k<n ; k++)
C[i][j]+=(A[i][k]*B[k][j]);
};
};
return 0;
};

/*
mat_print:
A matrix output function. for debugging.
*/

int mat_print(int **A , int n)
{
int i,j,k;
if (!A || n<1) return -1;
for (i=0 ; i<n ; i++)
{
for (j=0 ; j<n ; j++)
printf(" %d ",A[i][j]);
printf("\n");
}
return 0;
};
  • 打赏
  • 举报
回复
要求很高的话,用足够多比较随机的数据做种子,如100次鼠标移动的坐标,时间,100次用户的键盘输入的键值和时间等,最好能加上如CPU当前温度啊什么的等作为随机数种子,用MD5 ,SHA等散列算法对种子进行散列,散列值作为随机数输出并作为新的种子.一般的加密库都使用类似的方式产生随机数.
beepbug 2004-11-18
  • 打赏
  • 举报
回复
请教xuzheng318:
就几个数值,盘来盘去,能出随机数吗?
dlyy 2004-11-17
  • 打赏
  • 举报
回复
时间作种子还差不多,写个比较复杂的处理
Flood1984 2004-11-17
  • 打赏
  • 举报
回复
高在哪儿?
这在某台机子上被编译以后,就永远返回固定的值了
iamroc 2004-11-17
  • 打赏
  • 举报
回复
高,能说下原理吗?
xuzheng318 2004-11-17
  • 打赏
  • 举报
回复
#include<iostream.h>

static long int seed = 1L;
static long int const a =16807L, m = 2147483647L, q = 127773L, r = 2836L;
double Rondom()
{
seed = a*(seed%q) - r*(seed/q);
if( seed < 0 )
seed += m;
return (double) seed / (double) m;
}

int main()
{
double k=Rondom();
cout<<k<<endl;
}

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧