简体   繁体   中英

How to generate 4 digit numbers randomly?

I am using this piece of code:

byte[] intBytes = new byte[4];

rnd.GetBytes(intBytes);

return Math.Abs(BitConverter.ToInt32(intBytes, 0)) % maxValue + 1;

The above code only generates 3 digits. I would like for it to generate 4 digits?

I have changed the byte[4] to byte[5] but still only generates 3 digits?

What about?

Random random = new Random();
int randomNumber = random.Next(1000, 10000);

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