Randomly generated number

Selim Öztürk :

I am trying to make my own random number generator, but I was constantly getting the same values, I solved this with the sleep function, but how can I do without using sleep please help, I shared my code below.

My Random class

private int x;
private long ms;
public int random(int a) throws InterruptedException
{
    ms = System.currentTimeMillis();
    x=(int)(ms%a);
    Thread.sleep(1);
    return (int)x;
}

Main

 for(int i=0;i<100;i++)
    {
        System.out.println(rnd.random(10));
    }
WJS :

As has been stated, it won't be nearly as random as those methods provided by the Java API. But try using:

System.nanoTime();

It changes more rapidly.

Note: Writing a good RNG is non-trivial. Check out Random Number Generation for more info.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=388500&siteId=1