5

What is the best way to generate random a string which is composed of alphabets and its a maximum of 8million characters which will be tested using string searching algorithms? is Math.random still be ok for the randomness or the reliability of the spread of characters based on statistics? any comment is appreciated, right me if im wrong with my ideas.

verbatim64x
  • 51
  • 1
  • 1
  • 2

4 Answers4

3

How to generate a random alpha-numeric string?

Community
  • 1
  • 1
Michael D. Irizarry
  • 6,186
  • 5
  • 30
  • 35
1

Sure, why not? 8 MiB isn't that much, actually. Even bad PRNGs have periods at least of a few billion and Java uses an 48-bit LCG. So yes, it should be ok.

Joey
  • 344,408
  • 85
  • 689
  • 683
1

It depends entirely on the purpose of generating this string. If you're generating strings in order to test the performance of a search algorithm then you may want to generate "English-like" text containing a distribution of words similar to a typical document.

One way to achieve this would be to build a Markov Chain, whereby for each state you generate a given word; e.g. "The" and then transition to a new state with a certain probability; e.g. "The" -> "first". You could auto-generate the Markov chain using a large body of sample text, such as the Brown Corpus.

Or even simpler, you could test your algorithm using a particular corpus (such as the Brown Corpus) rather than having to generate any samples yourself.

Adamski
  • 54,009
  • 15
  • 113
  • 152
0

This class of commons-lang library does that job

org.apache.commons.lang.RandomStringUtils

You can use method "random"

String s = org.apache.commons.lang.RandomStringUtils.random(5, true, false);
dlopezgonzalez
  • 4,217
  • 5
  • 31
  • 42