用C随机数的全部变化 [英] Full variation of Random numbers in C

查看:123
本文介绍了用C随机数的全部变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成使用以下code 64位的随机数。我想在二进制数字,但问题是我不能让所有位变化。我想要的号码,以改变尽可能

I am trying to generate 64 bit random numbers using the following code. I want the numbers in binary,but the problem is I cant get all the bits to vary. I want the numbers to vary as much as possible

void PrintDoubleAsCBytes(double d, FILE* f)
{

f = fopen("tb.txt","a");

  unsigned char a[sizeof(d)];
  unsigned i;
  memcpy(a, &d, sizeof(d));
  for (i = 0; i < sizeof(a); i++){
    fprintf(f, "%0*X", (CHAR_BIT + 3) / 4, a[sizeof(d)-1-i]);

  }
   fprintf(f,"\n");
 fclose(f); /*done!*/
}

int main (int argc, char *argv)
{

int limit = 100 ;
double a, b;                
double result;
int i ;           
printf("limit = %d", limit );

for (i= 0 ; i< limit;i++)
    {
    a= rand();
    b= rand();
    result = a * b;
    printf ("A= %f B = %f\n",a,b);
    printf ("result= %f\n",result);
    PrintDoubleAsCBytes(a, stdout); puts("");
    PrintDoubleAsCBytes(b, stdout); puts("");
    PrintDoubleAsCBytes(result, stdout); puts("");

    }
}

输出文件

41DAE2D159C00000        //Last bits remain zero, I want them to change as well as in case of the result
41C93D91E3000000
43B534EE7FAEB1C3
41D90F261A400000
41D98CD21CC00000
43C4021C95228080
41DD2C3714400000
41B9495CFF000000
43A70D6CAD0EE321

我如何做我做到这一点?我没有在软件编码了丰富的经验。

How do I do I achieve this?I do not have much experience in software coding

推荐答案

在Java中是很容易的:

In Java it is very easy:

Random rng = new Random(); //do this only once

long randLong = rng.NextLong();
double randDoubleFromBits = Double.longBitsToDouble(randLong);

在C I只知道一个黑客的方法来做到这一点:)

In C I only know of a hack way to do it :)

由于RAND_MAX可以低至2 ^ 15-1,但被定义的实现,也许你可以得到64个随机比特出兰特()做面具和bitshifts

Since RAND_MAX can be as low as 2^15-1 but is implementation defined, maybe you can get 64 random bits out of rand() by doing masks and bitshifts:

//seed program once at the start
srand(time(NULL));

uint64_t a = rand()&0x7FFF;
uint64_t b = rand()&0x7FFF;
uint64_t c = rand()&0x7FFF;
uint64_t d = rand()&0x7FFF;
uint64_t e = rand()&0x7FFF;
uint64_t random = (a<<60)+(b<<45)+(c<<30)+(d<<15)+e;

然后东西在一个工会,并使用联盟其他成员间preT其位作为双击。类似

union
{
    double d;
    long l;
} doubleOrLong;

doubleOrLong.l = random;
double randomDouble = doubleOrLong.d;

(我没有测试此code)

(I haven't tested this code)

编辑:它应该如何工作说明

Explanation of how it should work

首先,函数srand(时间(NULL)); 种子兰特与当前的时间戳。所以,你只需要在开始一次做到​​这一点,如果你想重现先前的RNG系列,如果你喜欢,你可以重复使用的种子。

First, srand(time(NULL)); seeds rand with the current timestamp. So you only need to do this once at the start, and if you want to reproduce an earlier RNG series you can reuse that seed if you like.

兰特()返回一个0到RAND_MAX(含)之间的随机,公正的整数。 RAND_MAX保证是至少2 ^ 15-1,这是0x7FFF的。写程序使得不要紧RAND_MAX是什么(例如,它可以是2 ^ 16-1,2 ^ 31-1,2 ^ 32-1 ...),我们遮住所有但底部15位 - 0x7FFF的是0111 1111 1111 1111二进制,或者底部的15位

rand() returns a random, unbiased integer between 0 and RAND_MAX inclusive. RAND_MAX is guaranteed to be at least 2^15-1, which is 0x7FFF. To write the program such that it doesn't matter what RAND_MAX is (for example, it could be 2^16-1, 2^31-1, 2^32-1...), we mask out all but the bottom 15 bits - 0x7FFF is 0111 1111 1111 1111 in binary, or the bottom 15 bits.

现在,我们有我们所有15个随机比特包成64位。该bitshift运营商,&LT;&LT; ,移动左操作数(右操作数)位的左边。所以我们称之为随机最后uint64_t中有来自其他来源的变量,像这样的随机位:

Now we have to pack all of our 15 random bits into 64 bits. The bitshift operator, <<, shifts the left operand (right operand) bits to the left. So the final uint64_t we call random has random bits derived from the other variables like so:

AAAA BBBB BBBB BBBB CCCC BBBC CCCC CCCC DDDD CCDD DDDD DDDD DEEE EEEE EEEE EEEE

但是,这仍然被视为uint64_t中,而不是作为一个双。这是不确定的行为,这样做,所以你应该确保它正常工作,你希望你所选择的编译器的方式,但如果你把这个uint64_t中的工会,然后读取联盟的其他双成员,那么你会(希望!)除preT那些相同的位作为由随机位双。

But this is still being treated as a uint64_t, not as a double. It's undefined behaviour to do so, so you should make sure it works the way you expect on your compiler of choice, but if you put this uint64_t in a union and then read the union's other double member, then you'll (hopefully!) interpret those same bits as a double made up of random bits.

这篇关于用C随机数的全部变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆