True random generation in Java

后端 未结 13 2262
一生所求
一生所求 2020-12-01 01:18

I was reading the Math.random() javadoc and saw that random is only psuedorandom.

Is there a library (specifically java) that generates random numbers according to

相关标签:
13条回答
  • 2020-12-01 02:23

    Quick and dirty:

    public static int generateRandom() throws IOException
    {
        int num = 0;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
        for (int i = 0 ; i < Integer.SIZE ; i++)
        {
            System.out
              .println("Flip a fair coin. Enter h for heads, anything else for tails.");
    
            if (br.readLine().charAt(0) == 'h')
            {
                num += Math.pow(2, i);
            }
        }
    
        return num;
    }
    
    0 讨论(0)
提交回复
热议问题