Created
December 19, 2019 16:12
-
-
Save sonnyksimon/ac6d6150999c0aa73df86ed23cf19ec8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// -*- coding: utf-8 -*- | |
/* | |
rmake | |
~~~~~ | |
A random string generator, with 62 distinct characters | |
and upto 62^N possible values. If 10^6 values | |
are already generated, there's a 10^6/62^N | |
chance of collision. | |
:copyright: 2019 Pancubs.org | |
:license: MIT | |
*/ | |
import java.util.Random; | |
public class rmake { | |
public static String make(Integer N) { | |
/*Returns a random string of length N. Possible characters include | |
lowercase letters, uppercase letters, and decimal numbers. | |
String myid = rmake.make(); | |
.. versionadded: 0.1 | |
The first version. Uses java.util.Random. | |
:param N: the size of the random string. | |
*/ | |
//: The object that defines what possible characters | |
//: are generated. | |
char[] _chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); | |
Random random = new Random(); | |
char[] buf = new char[N]; | |
for (int idx = 0; idx < buf.length; ++idx) | |
buf[idx] = _chars[random.nextInt(_chars.length)]; | |
return new String(buf); | |
} | |
public static String make() { | |
return make(20); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment