Java Random String randomFileString()

Here you can find the source of randomFileString()

Description

random File String

License

Open Source License

Return

semi-random String of length 6 usable as filename suffix

Declaration

static String randomFileString() 

Method Source Code

//package com.java2s;
/* *******************************************************************
 * Copyright (c) 1999-2001 Xerox Corporation, 
 *               2002 Palo Alto Research Center, Incorporated (PARC).
 * All rights reserved. //w ww  .  j ava2  s  .co  m
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://www.eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *     Xerox/PARC     initial implementation 
 * ******************************************************************/

public class Main {
    final static String FILECHARS = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    /** @return semi-random String of length 6 usable as filename suffix */
    static String randomFileString() {
        final double FILECHARS_length = FILECHARS.length();
        final int LEN = 6;
        final char[] result = new char[LEN];
        int index = (int) (Math.random() * 6d);
        for (int i = 0; i < LEN; i++) {
            if (index >= LEN) {
                index = 0;
            }
            result[index++] = FILECHARS.charAt((int) (Math.random() * FILECHARS_length));
        }
        return new String(result);
    }
}

Related

  1. getString(int length)
  2. getString(int length)
  3. getString(int length)
  4. getString(int n, int arg[])
  5. randomCharacter(String availableValues)
  6. randomFixedByteLengthUnicodeString(Random r, int length)
  7. randomFixedLengthUnicodeString(Random random, char[] chars, int offset, int length)
  8. randomize(String[] orderedList)
  9. randomizeMap(Map map)