Skip to content

Instantly share code, notes, and snippets.

@sonnyksimon
Created December 19, 2019 16:12
Show Gist options
  • Save sonnyksimon/ac6d6150999c0aa73df86ed23cf19ec8 to your computer and use it in GitHub Desktop.
Save sonnyksimon/ac6d6150999c0aa73df86ed23cf19ec8 to your computer and use it in GitHub Desktop.
// -*- 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