「Java」SecureRandomクラスのnextInt()メソッドで乱数を生成するサンプル

アルゴリズムSHA1PRNG説明
Sunプロバイダが提供する擬似乱数生成(PRNG)アルゴリズム。このアルゴリズムは、PRNGの基盤としてSHA-1を使用します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study.javlesson;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class SecureRandomValDemo {
public static final String alogrithm = "SHA1PRNG";
public static void main(String[] args) throws NoSuchAlgorithmException {
SecureRandom srd;
srd = SecureRandom.getInstance(alogrithm);
System.out.println("0~20の数値の生成結果:\n" + srd.nextInt(20));
System.out.println("5~8の数値の生成結果:\n" + (srd.nextInt(5) + 3));
}
}
package com.arkgame.study.javlesson; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; public class SecureRandomValDemo { public static final String alogrithm = "SHA1PRNG"; public static void main(String[] args) throws NoSuchAlgorithmException { SecureRandom srd; srd = SecureRandom.getInstance(alogrithm); System.out.println("0~20の数値の生成結果:\n" + srd.nextInt(20)); System.out.println("5~8の数値の生成結果:\n" + (srd.nextInt(5) + 3)); } }
package com.arkgame.study.javlesson;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class SecureRandomValDemo {

      public static final String alogrithm = "SHA1PRNG";

      public static void main(String[] args) throws NoSuchAlgorithmException {
            SecureRandom srd;
            srd = SecureRandom.getInstance(alogrithm);

            System.out.println("0~20の数値の生成結果:\n" + srd.nextInt(20));

            System.out.println("5~8の数値の生成結果:\n" + (srd.nextInt(5) + 3));
      }

}

実行結果
0~20の数値の生成結果:
12
5~8の数値の生成結果:
6

Java

Posted by arkgame