Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm sorry but the code is too poorly written to be salvageable. I recommend something like this.</p> <pre><code> Random r = new Random(); // just create one and keep it around String alphabet = "abcdefghijklmnopqrstuvwxyz"; final int N = 10; StringBuilder sb = new StringBuilder(); for (int i = 0; i &lt; N; i++) { sb.append(alphabet.charAt(r.nextInt(alphabet.length()))); } String randomName = sb.toString(); System.out.println(randomName); </code></pre> <p>Key points are:</p> <ul> <li>Use <a href="http://java.sun.com/javase/6/docs/api/java/util/Random.html" rel="nofollow noreferrer"><code>java.util.Random</code></a>, specifically <code>nextInt(int n)</code> to get a random <code>int</code> in a given range <ul> <li>No need for funky formulas</li> </ul></li> <li>When building a string in a loop, use <code>StringBuilder</code>.</li> <li>Use an alphabet string, and <code>charAt</code> to index its letters.</li> </ul> <h3>API links</h3> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/Random.htmlhttp://java.sun.com/javase/6/docs/api/java/util/Random.html" rel="nofollow noreferrer"><code>java.util.Random</code></a> <ul> <li><a href="http://java.sun.com/javase/6/docs/api/java/util/Random.html#nextInt%28int%29" rel="nofollow noreferrer"><code>int nextInt(int n)</code></a> <ul> <li>Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive)</li> </ul></li> </ul></li> <li><a href="http://java.sun.com/javase/6/docs/api/java/lang/StringBuilder.html" rel="nofollow noreferrer"><code>StringBuilder</code></a> - A mutable sequence of characters.</li> </ul> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/73883/string-vs-stringbuilder">String vs StringBuilder (C#)</a></li> <li><a href="https://stackoverflow.com/questions/2971315/string-stringbuffer-and-stringbuilder/">String, StringBuffer and StringBuilder (Java)</a></li> </ul> <hr> <h3>Problems with the original code</h3> <p>There are plenty, unfortunately.</p> <ul> <li><code>String +=</code> in a loop yields very poor performance for longer strings</li> <li><code>for (int j = 0; j &lt;= lengthOfName; j++)</code> is an <a href="http://en.wikipedia.org/wiki/Off-by-one_error" rel="nofollow noreferrer">off-by-one-error</a></li> <li><code>freq == 7 &amp;&amp; freq == 8</code> is a logical contradiction</li> <li>It's just unnecessarily verbose! <ul> <li>Warning signs should go off whenever you write something like that</li> </ul></li> </ul> <p>I highly recommend doing lots of small but simple exercises to learn Java basics. <a href="http://codingbat.com/java" rel="nofollow noreferrer">codingbat.com</a> is great; it has hundreds of these, they're automatically graded so you'll know when your solution works as expected or not. It has sections on logic, strings, arrays, etc.</p> <hr> <h3>On uneven letter distribution</h3> <p>The simplest solution is to just have duplicates in the alphabet:</p> <ul> <li><code>String alphabet = "aab";</code> will have probability for <code>a</code> twice as much as <code>b</code></li> <li>You can generate the <code>alphabet</code> programmatically from a frequency table <ul> <li>I'll leave this as an exercise (or you can ask another question if you need it)</li> </ul></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload