C++11 random numbers

前端 未结 3 1079
夕颜
夕颜 2020-12-05 07:29

I need to generate random numbers, but from as wide a range as possible (64 bit at least). I don\'t care if the distribution is perfect, so std::rand() would wo

3条回答
  •  -上瘾入骨i
    2020-12-05 08:31

    Not C++11, but easy enough

    ((unsigned long long)rand() << 32) + rand() Here we generate two parts of int64 as int32's

    As JasonD pointed out, it assumes that rand() generate 32bit integer. It' spossible to xor rand() << x, rand() << (2*x), rand() << (3*x), etc, where x <= bit's in generate by rand() number`. It should be OK too.

提交回复
热议问题