创建一个 128 字节的随机数 [英] Create a 128 byte random number

查看:75
本文介绍了创建一个 128 字节的随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果 rand() 函数创建一个长度为 4 个字节的随机数,而我想创建一个长度为 1024 位(128 个字节)的随机数,这是最简单的方法通过将 rand() 函数连接 256 次来获得这个,还是有其他方法?

If the rand() function creates a random number that is 4 bytes in length, and I wanted to create a random number that is 1024 bits in length (128 bytes), is the easiest method to get this by concatenating the rand() function 256 times or is there an alternative method?

#include <stdio.h>
#include <string.h>

int main(void) {
  const char data[128];
  memset(&data, 0x36, 128);

  printf("%s\n", data);
  puts("");
  printf("%d\n", sizeof(data)/sizeof(data[0]));
  puts("");

  int i = 0;
  unsigned long rez = 0;

  for(i = 0; i < 20; i++) {
      unsigned int num = rand();
      rez = rez + num;
      printf("%x\n", rez);
  }

  printf("%x\n", rez);
  return 0;
}

推荐答案

将 rand() 函数连接 256 次是最简单的方法还是有其他方法?

is the easiest method to get this by concatenating the rand() function 256 times or is there an alternative method?

每个 rand() 返回 [0...RAND_MAX] 范围内的一个值.RAND_MAX 仅限于 32767 <= RAND_MAX <= INT_MAX.

Each rand() returns a value in the [0...RAND_MAX] range. RAND_MAX is limited to 32767 <= RAND_MAX <= INT_MAX.

很常见的RAND_MAX梅森数的形式2n - 1. 代码可以利用这个非常常见的实现依赖值.每个 rand() 调用然后提供 RAND_MAX_BITS 而不是 OP 为 4 字节 int 建议的 32.@Matteo Italia

Very commonly RAND_MAX is a Mersenne number of the form 2n − 1. Code can take advantage of this this very common implementation dependent value. Each rand() call then provides RAND_MAX_BITS and not 32 as suggested by OP for a 4-byte int. @Matteo Italia

[远见下方更新]

#include <stdlib.h>

#if RAND_MAX == 0x7FFF
#define RAND_MAX_BITS 15
#elif RAND_MAX == 0x7FFFFFFF
#define RAND_MAX_BITS 31
#else
#error TBD code
#endif

调用 rand() ⌈size * 8/RAND_MAX_BITS⌉ 次.这减少了 size 所需的 rand() 调用次数.

Call rand() ⌈size * 8 / RAND_MAX_BITS⌉ times. This eases the number of rand() calls needed from size.

void rand_byte(uint8_t *dest, size_t size) {
  int r_queue = 0;
  int r_bit_count = 0;
  for (size_t i = 0; i < size; i++) {
    int r = 0;
    //printf("%3zu %2d %8x\n", i, r_bit_count, r_queue);
    if (r_bit_count < 8) {
      int need = 8 - r_bit_count;
      r = r_queue << need;
      r_queue = rand();
      r ^= r_queue;  // OK to flip bits already saved in `r`
      r_queue >>= need;
      r_bit_count = RAND_MAX_BITS - need;
    } else {
      r = r_queue;
      r_queue >>= 8;
      r_bit_count -= 8;
    }
    dest[i] = r;
  }
}

int main(void) {
  uint8_t buf[128];
  rand_byte(buf, sizeof buf);
  ...
  return 0;
}

如果您想要最简单但效率稍低的代码,只需按照 @dbush

If you want the easiest bit less efficient code, simply call rand() for each byte as answered by @dbush

[2021 年更新]

@Anonymous Question Guy 发布了一个漂亮的宏,它返回 梅森数,比上面的 #if RAND_MAX == 0x7FFF 方法更普遍.

@Anonymous Question Guy posted a nifty macro that returns the bit width of a Mersenne number, more generally than the #if RAND_MAX == 0x7FFF approach above.

/* Number of bits in inttype_MAX, or in any (1<<b)-1 where 0 <= b < 3E+10 */
#define IMAX_BITS(m) ((m) /((m)%0x3fffffffL+1) /0x3fffffffL %0x3fffffffL *30 \
              + (m)%0x3fffffffL /((m)%31+1)/31%31*5 + 4-12/((m)%31+3))

_Static_assert((RAND_MAX & 1 && (RAND_MAX/2 + 1) & (RAND_MAX/2)) == 0, 
    "RAND_MAX is not a Mersenne number");
#define RAND_MAX_BITS IMAX_BITS(RAND_MAX)

这篇关于创建一个 128 字节的随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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