Java Random Number createParameters(int numberOfParameters)

Here you can find the source of createParameters(int numberOfParameters)

Description

create Parameters

License

Open Source License

Declaration

public static Map<String, String> createParameters(int numberOfParameters) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class Main {
    private static final char[] ALPHABET = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
    private static final Random GENERATOR = new Random();

    public static Map<String, String> createParameters(int numberOfParameters) {
        int defaultKeyLength = 8;
        // in rare case that number of parameters is greater than maximum of
        // possible keys
        while (numberOfParameters > Math.pow(ALPHABET.length, defaultKeyLength)) {
            defaultKeyLength *= 2;/*from  w  ww. ja  va2s . co  m*/
        }

        Map<String, String> parameters = new HashMap<>();
        for (int i = 0; i < numberOfParameters; i++) {
            parameters.put(generateAlphabetString(defaultKeyLength), generateAlphabetString(defaultKeyLength));
        }
        return parameters;
    }

    public static String generateAlphabetString(int length) {
        char[] text = new char[length];
        for (int i = 0; i < length; i++) {
            text[i] = ALPHABET[GENERATOR.nextInt(ALPHABET.length)];
        }
        return new String(text);
    }
}

Related

  1. createRandomFilename()
  2. createRandomHexNumber(int length)
  3. createRandomNumber()
  4. createRandomNumber(int index)