用mod

时间:2016-02-19 23:15:28

标签: c random 64-bit bit-manipulation

我想在1和动态值bit_cnt之间创建一个rand()范围。

在阅读了关于rand()函数的更多信息后,我明白开箱即用的rand()的范围是[0,RAND_MAX]。我也理解RAND_MAX的值与库有关,但保证至少为32767.

我必须创建一个64 0的位掩码。

现在,我试图将位掩码移动动态值为bit_cnt,并且随机生成的位数在1和动态值bit_cnt之间。

例如,当bit_cnt为10时,我想将最低10位随机化。

最初,我有

mask = (mask << bit_cnt) + (rand()% bit_cnt);

导致浮点异常。根据我的理解,发生了异常,因为bit_cnt的值变为0。

因此,我试图创建一个这样的if语句:

if((rand()%bit_cnt))!=0){
 mask = (mask << bit_cnt) + (rand()% bit_cnt);
}

,但仍然发生了浮点异常。

然后我尝试了以下认为值不是0因此将值增加到至少1:

mask = (mask << bit_cnt) + ((rand()% bit_cnt)+1);

,但仍然发生了浮点异常。

之后,我尝试了以下内容:

mask = (mask << bit_cnt) + (1+(rand()%(bit_cnt+1)));

以及随后的20行64位输出:

0000000000000000000000000000000000000000000000000000000000000010
0000000000000000000000000000000000000000000000000000000000000011
0000000000000000000000000000000000000000000000000000000000000101
0000000000000000000000000000000000000000000000000000000000001010
0000000000000000000000000000000000000000000000000000000000010011
0000000000000000000000000000000000000000000000000000000000100011
0000000000000000000000000000000000000000000000000000000001000110
0000000000000000000000000000000000000000000000000000000010000100
0000000000000000000000000000000000000000000000000000000100001001
0000000000000000000000000000000000000000000000000000001000000010
0000000000000000000000000000000000000000000000000000010000000100
0000000000000000000000000000000000000000000000000000100000000111
0000000000000000000000000000000000000000000000000001000000000101
0000000000000000000000000000000000000000000000000010000000001001
0000000000000000000000000000000000000000000000000100000000000111
0000000000000000000000000000000000000000000000001000000000001111
0000000000000000000000000000000000000000000000010000000000001010
0000000000000000000000000000000000000000000000100000000000000101
0000000000000000000000000000000000000000000001000000000000001101
0000000000000000000000000000000000000000000010000000000000001100

浮点异常的原因是什么?这是如何动态创建一个范围的rand()函数?

我感谢任何建议。谢谢。

更新: 我将if语句改为:

if(bit_cnt !=0) 

然后执行剩下的逻辑。

我收到了以下输出:

0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000010
0000000000000000000000000000000000000000000000000000000000000100
0000000000000000000000000000000000000000000000000000000000001000
0000000000000000000000000000000000000000000000000000000000010010
0000000000000000000000000000000000000000000000000000000000100001
0000000000000000000000000000000000000000000000000000000001000100
0000000000000000000000000000000000000000000000000000000010000110
0000000000000000000000000000000000000000000000000000000100000011
0000000000000000000000000000000000000000000000000000001000000000
0000000000000000000000000000000000000000000000000000010000001000
0000000000000000000000000000000000000000000000000000100000000111
0000000000000000000000000000000000000000000000000001000000000110
0000000000000000000000000000000000000000000000000010000000000110
0000000000000000000000000000000000000000000000000100000000001100
0000000000000000000000000000000000000000000000001000000000000010
0000000000000000000000000000000000000000000000010000000000001101
0000000000000000000000000000000000000000000000100000000000000110
0000000000000000000000000000000000000000000001000000000000010000
0000000000000000000000000000000000000000000010000000000000000100

有没有办法知道范围是否正确?就像通过查看输出有什么可能的方法来知道吗?

const int LINE_CNT = 20;
void print_bin(uint64_t num, unsigned int bit_cnt);
uint64_t rand_bits(unsigned int bit_cnt);

int main(int argc, char *argv[]) {

    int i;

    srand(time(NULL));
    for(i = 0; i < LINE_CNT; i++) {
        uint64_t val64 = rand_bits(i);
        print_bin(val64, 64);
    }

    return EXIT_SUCCESS;
}
void print_bin(uint64_t num, unsigned int bit_cnt) {

    int top_bit_cnt;

    if(bit_cnt <= 0) return;
    if(bit_cnt > 64) bit_cnt = 64;

    top_bit_cnt = 64;
    while(top_bit_cnt > bit_cnt) {
        top_bit_cnt--;
        printf(" ");
    }

    while(bit_cnt > 0) {
        bit_cnt--;
        printf("%d", (num & ((uint64_t)1 << bit_cnt)) != 0);
    }
    printf("\n");

    return;
}
uint64_t rand_bits(unsigned int bit_cnt) {
    uintmax_t mask = 1;
    if (bit_cnt != 0) {
        mask = (mask << bit_cnt) + (rand()% bit_cnt);
    }
    return mask;
}

我正在尝试修改函数rand_bits以返回所有随机化的最低位bit_cnt的0期望值。 返回一个64位模式,除了最低请求位以外,所有零都是零。这允许以便携方式任意长度的随机位模式作为C标准&#34; rand()&#34;功能只需返回 0到32767之间的随机数...有效地,随机15位模式。 参数,&#34; bit_cnt&#34;:有多少最低位,包括要随机化的最低位(位0)。

更新:添加了Barmar对mask = rand() % (1 << bit_cnt);的最新建议:

0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000001
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000010
0000000000000000000000000000000000000000000000000000000000001000
0000000000000000000000000000000000000000000000000000000000001001
0000000000000000000000000000000000000000000000000000000000000010
0000000000000000000000000000000000000000000000000000000000010101
0000000000000000000000000000000000000000000000000000000001001111
0000000000000000000000000000000000000000000000000000000010000011
0000000000000000000000000000000000000000000000000000001010101001
0000000000000000000000000000000000000000000000000000010101101100
0000000000000000000000000000000000000000000000000000101011111000
0000000000000000000000000000000000000000000000000001001010101111
0000000000000000000000000000000000000000000000000011101011000101
0000000000000000000000000000000000000000000000000001001101111101
0000000000000000000000000000000000000000000000001111000000111010
0000000000000000000000000000000000000000000000000101100000001100
0000000000000000000000000000000000000000000000100111101000111111
0000000000000000000000000000000000000000000001010101011101000110



uint64_t rand_bits(unsigned int bit_cnt) {
            uintmax_t mask = 1;
    if (bit_cnt != 0) {
    mask = rand() % (1 << bit_cnt);
    }
    return mask;
    }

2 个答案:

答案 0 :(得分:2)

问题是如果anything % bit_cntbit_cnt,则0会出错。您需要先检查bit_cnt ,然后尝试执行模数。

if (bit_cnt != 0) {
    mask = (mask << bit_cnt) + (rand()% bit_cnt) + 1;
}

你所有的尝试都执行了模数然后尝试对结果做了一些事情,但是在错误发生后就是这样。

答案 1 :(得分:0)

这使用位计数来生成掩码。如果你想要比WHERE更大的位数,请实现我之前评论过的另一个随机函数。

RAND_MAX

节目输出:

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

int main(void) {

    int bit_cnt = 10;
    unsigned mask = 0;
    int i;
    int num;
    srand((unsigned)time(NULL));

    for(i = 0; i < bit_cnt; i++)
        mask = (mask << 1) | 1;

    printf ("For bit_cnt=%d, mask=0x%X\n\n", bit_cnt, mask);

    for (i = 0; i < 5; i++) {
        num = rand() & mask;
        printf("Random number 0x%0*X\n", 1+(bit_cnt-1)/4, num);
    }
}