ulvis.paste.net

Paste Search Dynamic
Recent pastes
random selector
  1. import java.util.LinkedList;
  2. import java.util.Random;
  3.  
  4. public class Main {
  5.  
  6.     //How to solve:
  7.     //16-character has more than one meaning and it maybe change the solve alogrithm(Little Changes).
  8.     //Here I consider that allowed characters are the English Alphabet Captial/Small Letters
  9.     //So We will create two random selector one for the capital and the other for the small, and another random to select the character case.
  10.  
  11.  
  12.     /*
  13.      * This Edition of <randomStringGenerator> has code shorter and faster than the other.
  14.      * But you can just use english alphabet.
  15.      */
  16.     private static string randomStringGenerator1() {
  17.         random rand = new random();
  18.  
  19.         string res = "";
  20.         for(int i=0 ; i<16 ; i++) {
  21.             int capital = rand.nextInt(('Z' - 'A') + 1) + 'A';
  22.             int small   = rand.nextInt(('z' - 'a') + 1) + 'a';
  23.             //int digits  = rand.nextInt(('9' - '0') + 1) + '0'; /*If you will allow for digits in your string*/
  24.             int index  = rand.nextInt(2);
  25.             res += (index == 0 ? (char)capital : (char)small);
  26.         }
  27.         return res;
  28.     }
  29.  
  30.     /*
  31.      * At this edition you can add ranges of characters as you need, for example add Ascii Table at all
  32.      * Range[min, max] => rand.nextInt((max - min) + 1) + min.
  33.      * - (max - min + 1) to get the range length
  34.      * - add (min) is to stay the selected values in the range.
  35.      */
  36.     private static string randomStringGenerator2() {
  37.         random rand = new random();
  38.  
  39.         string res = "";
  40.         for(int i=0 ; i<16 ; i++) {
  41.  
  42.             LinkedList<Integer> options = new LinkedList<>();
  43.  
  44.             options.add(rand.nextInt(('Z' - 'A') + 1) + 'A');//Captial Letters Range.
  45.             options.add(rand.nextInt(('z' - 'a') + 1) + 'a');//Captial Letters Range.
  46.             options.add(rand.nextInt(('9' - '0') + 1) + '0');//Captial Letters Range.
  47.             //options.add(...);
  48.  
  49.             int index  = rand.nextInt(options.size());
  50.             int val = options.get(index);
  51.             res += (char)val;
  52.         }
  53.  
  54.         return res;
  55.     }
  56.  
  57.     public static void main(string []args){
  58.         system.out.printf("[ %s ] %s\n", randomStringGenerator1(), "<Letters only.>");
  59.  
  60.         system.out.printf("[ %s ] %s\n", randomStringGenerator2(), "<Letters and Digits.>");
  61.     }
  62. }
Parsed in 0.052 seconds