本文整理匯總了Java中java.security.SecureRandom.nextInt方法的典型用法代碼示例。如果您正苦於以下問題:Java SecureRandom.nextInt方法的具體用法?Java SecureRandom.nextInt怎麽用?Java SecureRandom.nextInt使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.security.SecureRandom
的用法示例。
在下文中一共展示了SecureRandom.nextInt方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: generateRandomString
import java.security.SecureRandom; //導入方法依賴的package包/類
/*******************
* Generate a random alphanumeric string which is 8-32 characters long.
*
* @return A random string.
******************/
protected String generateRandomString() {
SecureRandom sr = new SecureRandom();
char[] chars = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".toCharArray();
String out = "";
int len;
//Build a random string
len = sr.nextInt(24) + 8;
for(int i = 0; i < len; ++i) {
out = out + chars[sr.nextInt(chars.length)];
}
//Return the string
return out;
}
示例2: main
import java.security.SecureRandom; //導入方法依賴的package包/類
public static void main(String[] args)
{
Tree<Integer> tree = new Tree<Integer>();
SecureRandom randomNumber = new SecureRandom();
System.out.println("Inserting the following values: ");
// insert 10 random integers from 0-99 in tree
for (int i = 1; i <= 10; i++)
{
int value = randomNumber.nextInt(100);
System.out.printf("%d ", value);
tree.insertNode(value);
}
System.out.printf("%n%nPreorder traversal%n");
tree.preorderTraversal();
System.out.printf("%n%nInorder traversal%n");
tree.inorderTraversal();
System.out.printf("%n%nPostorder traversal%n");
tree.postorderTraversal();
System.out.println();
}
示例3: main
import java.security.SecureRandom; //導入方法依賴的package包/類
public static void main(String[] args)
{
SecureRandom generator = new SecureRandom();
int[] data = new int[10]; // create array
for (int i = 0; i < data.length; i++) // populate array
data[i] = 10 + generator.nextInt(90);
System.out.printf("Unsorted array:%n%s%n%n",
Arrays.toString(data)); // display array
mergeSort(data); // sort array
System.out.printf("Sorted array:%n%s%n%n",
Arrays.toString(data)); // display array
}
示例4: main
import java.security.SecureRandom; //導入方法依賴的package包/類
public static void main(String[] args)
{
SecureRandom generator = new SecureRandom();
int[] data = new int[10]; // create array
for (int i = 0; i < data.length; i++) // populate array
data[i] = 10 + generator.nextInt(90);
System.out.printf("Unsorted array:%n%s%n%n",
Arrays.toString(data)); // display array
insertionSort(data); // sort array
System.out.printf("Sorted array:%n%s%n%n",
Arrays.toString(data)); // display array
}
示例5: setRandomImage
import java.security.SecureRandom; //導入方法依賴的package包/類
private void setRandomImage(ImageView image) {
SecureRandom imgGen = new SecureRandom();
switch (imgGen.nextInt(3)) {
case 0:
image.setImageResource(R.drawable.scn1);
break;
case 1:
image.setImageResource(R.drawable.jr13);
break;
case 2:
image.setImageResource(R.drawable.jr16);
break;
}
}
示例6: generateWebAuth
import java.security.SecureRandom; //導入方法依賴的package包/類
private static String generateWebAuth() {
String randomAllowed = "01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
char[] randomChars = randomAllowed.toCharArray();
char[] randomBuffer;
randomBuffer = new char[30];
SecureRandom random = new SecureRandom();
for (int i = 0; i < randomBuffer.length; i++) {
randomBuffer[i] = randomChars[random.nextInt(randomChars.length)];
}
return new String(randomBuffer);
}
示例7: SslNetworkLayer
import java.security.SecureRandom; //導入方法依賴的package包/類
public SslNetworkLayer(
SipStackImpl sipStack,
String trustStoreFile,
String keyStoreFile,
char[] keyStorePassword,
char[] trustStorePassword,
String keyStoreType, String trustStoreType) throws GeneralSecurityException, FileNotFoundException, IOException
{
SSLContext sslContext;
sslContext = SSLContext.getInstance("TLS");
String algorithm = KeyManagerFactory.getDefaultAlgorithm();
TrustManagerFactory tmFactory = TrustManagerFactory.getInstance(algorithm);
KeyManagerFactory kmFactory = KeyManagerFactory.getInstance(algorithm);
SecureRandom secureRandom = new SecureRandom();
secureRandom.nextInt();
KeyStore keyStore = KeyStore.getInstance(
keyStoreType != null ? keyStoreType : KeyStore.getDefaultType());
KeyStore trustStore = KeyStore.getInstance(
trustStoreType != null ? trustStoreType : KeyStore.getDefaultType());
keyStore.load(new FileInputStream(keyStoreFile), keyStorePassword);
trustStore.load(new FileInputStream(trustStoreFile), trustStorePassword);
tmFactory.init(trustStore);
kmFactory.init(keyStore, keyStorePassword);
if(sipStack.getClientAuth() == ClientAuthType.DisabledAll) {
if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
logger.logDebug(
"ClientAuth " + sipStack.getClientAuth() + " bypassing all cert validations");
}
sslContext.init(null, trustAllCerts, secureRandom);
} else {
if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
logger.logDebug(
"ClientAuth " + sipStack.getClientAuth());
}
sslContext.init(kmFactory.getKeyManagers(), tmFactory.getTrustManagers(), secureRandom);
}
sslServerSocketFactory = sslContext.getServerSocketFactory();
sslSocketFactory = sslContext.getSocketFactory();
}
示例8: generate
import java.security.SecureRandom; //導入方法依賴的package包/類
static String generate() {
char[] buf = new char[LENGTH];
SecureRandom random = RANDOM.get();
for (int i = 0; i < LENGTH; i++) {
buf[i] = symbols[random.nextInt(symbols.length)];
}
return new String(buf);
}
示例9: getPad
import java.security.SecureRandom; //導入方法依賴的package包/類
private static String getPad(int size) {
int tmp;
SecureRandom rnd = new SecureRandom();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; ++i) {
tmp = rnd.nextInt(alphabet.length());
sb.append(alphabet.toCharArray()[tmp]);
}
return sb.toString();
}
示例10: coinToss
import java.security.SecureRandom; //導入方法依賴的package包/類
private String coinToss() {
SecureRandom r = new SecureRandom();
int coin = r.nextInt(2);
if (coin == 0) {
return "HEADS";
} else {
return "TAILS";
}
}
示例11: generateRandom
import java.security.SecureRandom; //導入方法依賴的package包/類
/**
* Generates a polynomial with coefficients randomly selected from <code>{-1, 0, 1}</code>.
*
* @param N number of coefficients
*/
public static DenseTernaryPolynomial generateRandom(int N, SecureRandom random)
{
DenseTernaryPolynomial poly = new DenseTernaryPolynomial(N);
for (int i = 0; i < N; i++)
{
poly.coeffs[i] = random.nextInt(3) - 1;
}
return poly;
}
示例12: check
import java.security.SecureRandom; //導入方法依賴的package包/類
/**
* Verify the similarity of random numbers generated though both original
* as well as deserialized instance.
*/
private static void check(SecureRandom orig, SecureRandom copy,
boolean equal, String mech) {
int o = orig.nextInt();
int c = copy.nextInt();
System.out.printf("%nRandom number generated for mechanism: '%s' "
+ "from original instance as: '%s' and from serialized "
+ "instance as: '%s'", mech, o, c);
if (equal) {
Asserts.assertEquals(o, c, mech);
} else {
Asserts.assertNotEquals(o, c, mech);
}
}
示例13: generateBridgeSecret
import java.security.SecureRandom; //導入方法依賴的package包/類
/** Called by cordova.js to initialize the bridge. */
int generateBridgeSecret() {
SecureRandom randGen = new SecureRandom();
expectedBridgeSecret = randGen.nextInt(Integer.MAX_VALUE);
return expectedBridgeSecret;
}
示例14: main
import java.security.SecureRandom; //導入方法依賴的package包/類
public static void main(String[] args)
{
// randomNumbers object will produce secure random numbers
SecureRandom randomNumbers = new SecureRandom();
int frequency1 = 0; // count of 1s rolled
int frequency2 = 0; // count of 2s rolled
int frequency3 = 0; // count of 3s rolled
int frequency4 = 0; // count of 4s rolled
int frequency5 = 0; // count of 5s rolled
int frequency6 = 0; // count of 6s rolled
// tally counts for 6,000,000 rolls of a die
for (int roll = 1; roll <= 6000000; roll++)
{
int face = 1 + randomNumbers.nextInt(6); // number from 1 to 6
// use face value 1-6 to determine which counter to increment
switch (face)
{
case 1:
++frequency1; // increment the 1s counter
break;
case 2:
++frequency2; // increment the 2s counter
break;
case 3:
++frequency3; // increment the 3s counter
break;
case 4:
++frequency4; // increment the 4s counter
break;
case 5:
++frequency5; // increment the 5s counter
break;
case 6:
++frequency6; // increment the 6s counter
break;
}
}
System.out.println("Face\tFrequency"); // output headers
System.out.printf("1\t%d%n2\t%d%n3\t%d%n4\t%d%n5\t%d%n6\t%d%n",
frequency1, frequency2, frequency3, frequency4,
frequency5, frequency6);
}
示例15: Layer
import java.security.SecureRandom; //導入方法依賴的package包/類
/**
* This function generates the coefficients of all polynomials in this layer
* at random using random generator.
*
* @param sr the random generator which is to be used
*/
public Layer(int vi, int viNext, SecureRandom sr)
{
this.vi = vi;
this.viNext = viNext;
this.oi = viNext - vi;
// the coefficients of all polynomials in this layer
this.coeff_alpha = new short[this.oi][this.oi][this.vi];
this.coeff_beta = new short[this.oi][this.vi][this.vi];
this.coeff_gamma = new short[this.oi][this.viNext];
this.coeff_eta = new short[this.oi];
int numOfPoly = this.oi; // number of polynomials per layer
// Alpha coeffs
for (int k = 0; k < numOfPoly; k++)
{
for (int i = 0; i < this.oi; i++)
{
for (int j = 0; j < this.vi; j++)
{
coeff_alpha[k][i][j] = (short)(sr.nextInt() & GF2Field.MASK);
}
}
}
// Beta coeffs
for (int k = 0; k < numOfPoly; k++)
{
for (int i = 0; i < this.vi; i++)
{
for (int j = 0; j < this.vi; j++)
{
coeff_beta[k][i][j] = (short)(sr.nextInt() & GF2Field.MASK);
}
}
}
// Gamma coeffs
for (int k = 0; k < numOfPoly; k++)
{
for (int i = 0; i < this.viNext; i++)
{
coeff_gamma[k][i] = (short)(sr.nextInt() & GF2Field.MASK);
}
}
// Eta
for (int k = 0; k < numOfPoly; k++)
{
coeff_eta[k] = (short)(sr.nextInt() & GF2Field.MASK);
}
}