Here's a quick way to generate a short, random string in Java:
Random r = new Random(); String token = Long.toString(Math.abs(r.nextLong()), 36);
The resultant string is a maximum of ceil(ln(263) / ln(36)) = 13
characters long and is suitable for use as a temporary id or cookie name.
An even quicker and dirtier method is to simply convert a random double to a string:
String token = "token" + Math.random();
This is useful in test cases and one-offs, but I wouldn't put it into production code.
M | T | W | T | F | S | S |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 |