rand()不返回随机值 [英] rand() is not returning random values
问题描述
它是一个小代码,用于生成一个随机Hermitian矩阵。
在每次调用rand()之前,我都调用了srand()。但输出中仍然没有随机性。
我使用c99的复杂数据类型功能来创建厄米特矩阵。我不确定我错在哪里:($ / b>
#include< stdio.h>
#include < math.h>
#include< complex.h>
#include< stdlib.h>
#include< time.h>
#define MATSIZE 5
#define RAND_RANGE 100
double complex mat [MATSIZE] [MATSIZE];
void gen_mat()
{
; i = 0,j;
int real;
int img;
for(; i< MATSIZE; i ++)
{
srand(time(NULL ));
real = rand()%RAND_RANGE + 1;
srand(time(NULL));
img = rand()%RAND_RANGE + 1;
for(j = MATSIZE; j!= i; j--)
{
mat [i] [j] = real + img * I;
mat [j] [i] = conj(mat [i] [j]);
}
srand(time(NULL));
if(i == j)
mat [i] [i] = rand )%RAND_RANGE + 0 * I;
}
}
void print_mat()
{
int i,j;
for i = 0; i {
for(j = 0; j {
print f(%f +%f * i,creal(mat [i] [j]),cimag(mat [i] [j]));
printf();
}
puts(\\\
);
int main()
{
gen_mat();
print_mat();
返回0;
}
样本输出 在main()中调用srand的EDIT实际上解决了这个问题。 在每次调用 Its a small code to generate a random hermitian matrix hermitian matrix. I have called srand() before every call to rand(). but still no randomness in the output. I have used c99's complex datatype feature to create a hermitian matrix. I'm not sure where i'm wrong :( sample output EDIT calling srand in main() actually solved the problem. Thank you guys.
Don't call 这篇关于rand()不返回随机值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
<$ $ ./a.out
66.000000 + 0.000000 * i 67.000000 + 67.000000 * i 67.000000 + 67.000000 * i 67.000000 + 67.000000 * i 67.000000 + 67.000000 * i
67.000000 + -67.000000 * i 66.000000 + 0.000000 * i 67.000000 + 67.000000 * i 67.000000 + 67.000000 * i 67.000000 + 67.000000 * i
67.000000 +67.000000 * i 67.000000 + -67.000000 * i 66.000000 + 0.000000 * i 67.000000 + 67.000000 * i 67.000000 + 67.000000 * i
67.000000 + 67.000000 * i 67.000000 + -67.000000 * i 67.000000 + -67.000000 * i 66.000000 + 0.000000 * i 67.000000 + 67.000000 * i
67.000000 + 67.000000 * i 67.000000 + -67.000000 * i 67.000000 + -67.000000 * i 67.000000 + -67.000000 * i 66.000000 + 0.000000 * i
[aft @ centos-c physics-numaric] $ ./a.out
31.000000 + 0.000000 * i 81.000000 + 75.000000 * i 81.000000 + 75.000000 * i 81.000000 + 75.000000 * i 81.000000 + 75.000000 * i
81.000000 + -75.000000 * i 53.000000 + 0.000000 * i 69.000000 + 57.000000 * i 69.000000 + 57.000000 * i 69.000000 + 57.000000 * i
69.000000 + 57.000000 * i 69.000000 + -57.000000 * i 27.000000 + 0.000000 * i 93.000000 + 11.000000 * i 93.000000 + 11.000000 * i
93.000000 + 11.000000 * i 69.000000 + -57.000000 * i 93.000000 + -11.000000 * i 58.000000 + 0.000000 * i 76.000000 + 78.000000 * i
$ b $ 76.00 +78.000000 * i 69.000000 + -57.000000 * i 93.000000 + -11.000000 * i 76.000000 + -78.000000 * i 67.000000 + 0.000000 * i
rand
之前,请勿调用 srand
。在程序启动时调用一次。
#include <stdio.h>
#include <math.h>
#include <complex.h>
#include <stdlib.h>
#include <time.h>
#define MATSIZE 5
#define RAND_RANGE 100
double complex mat[MATSIZE][MATSIZE];
void gen_mat()
{
int i =0,j;
int real;
int img;
for( ;i < MATSIZE; i++)
{
srand(time(NULL));
real = rand()%RAND_RANGE + 1;
srand(time(NULL));
img = rand()%RAND_RANGE + 1;
for(j = MATSIZE; j != i ; j--)
{
mat[i][j] = real + img * I;
mat[j][i] = conj(mat[i][j]);
}
srand(time(NULL));
if(i == j)
mat[i][i] = rand()%RAND_RANGE + 0*I;
}
}
void print_mat()
{
int i,j;
for(i = 0; i < MATSIZE; i++)
{
for(j = 0; j < MATSIZE; j++)
{
printf("%f + %f *i", creal(mat[i][j]), cimag(mat[i][j]));
printf(" ");
}
puts("\n");
}
}
int main()
{
gen_mat();
print_mat();
return 0;
}
[aft@centos-c physics-numaric]$ ./a.out
66.000000 + 0.000000 *i 67.000000 + 67.000000 *i 67.000000 + 67.000000 *i 67.000000 + 67.000000 *i 67.000000 + 67.000000 *i
67.000000 + -67.000000 *i 66.000000 + 0.000000 *i 67.000000 + 67.000000 *i 67.000000 + 67.000000 *i 67.000000 + 67.000000 *i
67.000000 + 67.000000 *i 67.000000 + -67.000000 *i 66.000000 + 0.000000 *i 67.000000 + 67.000000 *i 67.000000 + 67.000000 *i
67.000000 + 67.000000 *i 67.000000 + -67.000000 *i 67.000000 + -67.000000 *i 66.000000 + 0.000000 *i 67.000000 + 67.000000 *i
67.000000 + 67.000000 *i 67.000000 + -67.000000 *i 67.000000 + -67.000000 *i 67.000000 + -67.000000 *i 66.000000 + 0.000000 *i
[aft@centos-c physics-numaric]$ ./a.out
31.000000 + 0.000000 *i 81.000000 + 75.000000 *i 81.000000 + 75.000000 *i 81.000000 + 75.000000 *i 81.000000 + 75.000000 *i
81.000000 + -75.000000 *i 53.000000 + 0.000000 *i 69.000000 + 57.000000 *i 69.000000 + 57.000000 *i 69.000000 + 57.000000 *i
69.000000 + 57.000000 *i 69.000000 + -57.000000 *i 27.000000 + 0.000000 *i 93.000000 + 11.000000 *i 93.000000 + 11.000000 *i
93.000000 + 11.000000 *i 69.000000 + -57.000000 *i 93.000000 + -11.000000 *i 58.000000 + 0.000000 *i 76.000000 + 78.000000 *i
76.000000 + 78.000000 *i 69.000000 + -57.000000 *i 93.000000 + -11.000000 *i 76.000000 + -78.000000 *i 67.000000 + 0.000000 *i
srand
before every call to rand
. Call it once when your program starts.