Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How should I generate a pseudo-random integer between 0 and n-1?

integer n-1 pseudo-random
0
Posted

How should I generate a pseudo-random integer between 0 and n-1?

0

import java.util.Random; … Random rand = new Random(seed); // or new Random() for a “random” seed rand.nextInt(n); Note: you should only create one object of type Random and only set the seed once, not one for each random number you need to generate. Unfortunately, the library random number generator is fairly poor so don’t use this if it really matters (e.g., cryptography or in a slot machine).

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123