Skip to content

Commit

Permalink
documented
Browse files Browse the repository at this point in the history
  • Loading branch information
kohsuke committed Apr 7, 2014
1 parent e339283 commit 9304cdb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
32 changes: 32 additions & 0 deletions README.md
@@ -0,0 +1,32 @@
# Human-friendly Random Name Generator

This little library generates human friendly random identifiers. For example, given the following
code,

RandomNameGenerator rnd = new RandomNameGenerator(0);

for (int i=0; i<10; i++)
System.out.println(rnd.next());

The output will be as follows:

constructive_carrot
flexible_designer
linear_fund
popular_leaf
steady_parent
abstract_rest
controversial_supply
fragrant_absorption
lively_cassette
powerful_destruction

In testing, these names are more useful than number-based random names, as they are more memorable to
humans.

The generator is based on a fixed dictionary of about 600 adjectives and 2400 nouns, thereby
producing little more than 1.5 million unique combinations. If you keep calling the `next()`
method beyond this limit, it'll start producing the same name again.

The generator is pseudo-random, meaning if you provide the same seed value, it'll always
generate the same sequence of identifiers.
5 changes: 4 additions & 1 deletion src/main/java/org/kohsuke/randname/RandomNameGenerator.java
Expand Up @@ -17,7 +17,10 @@ public RandomNameGenerator(int seed) {
this.pos = seed;
}

public String next() {
public RandomNameGenerator() {
this((int) System.currentTimeMillis());
}
public synchronized String next() {
Dictionary d = Dictionary.INSTANCE;
pos = Math.abs(pos+d.getPrime()) % d.size();
return d.word(pos);
Expand Down
Expand Up @@ -25,4 +25,10 @@ public void uniqueness() {

assertFalse(s.add(r.next()));
}

@Test
public void firstTen() {
for (int i=0; i<10; i++)
System.out.println(r.next());
}
}

0 comments on commit 9304cdb

Please sign in to comment.