简体   繁体   中英

Random number generator with range in Java

I have an array like:

byte[] a = new byte[4];

Random rnd;

    rnd.nextBytes(a);

I want to know if each element of this array named a in decimal is in range 0-255 or can be beyond this interval?

Java's primitive byte is a data type is an 8-bit signed two's complement integer.

That means the minimum value is -128 (-2^7) and the maximum value is 127 (inclusive)(2^7 -1)

if you need or want a range between 0 and 255 you will need to correct the random number by yourself.


And btw, Random class must be initialized before you do something like:

rnd.nextBytes(a);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM