Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a number of n-bit in length [closed]

Tags:

c++

algorithm

bit

I'm looking for an algorithm that allows me generate pseudo-random numbers, but.. How can I the interval of numbers of n-bits? if you have an example for c++ will be great

for example:

BigNumber random(bits 256)

how Can I get the least and the most number of 256 bits

Thanks to all guys, I found the solution

if I have n-bit, the numbers are between (2^n)/2 and (2^n)-1

like image 952
user17629 Avatar asked Dec 17 '25 15:12

user17629


1 Answers

If the type of random numbers is int, you could use the & operator to mask out unnecessary bits by using a suitable bit mask, e.g. binary 00001111 for the first four bits.

like image 115
Codor Avatar answered Dec 19 '25 06:12

Codor



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!