Generating * interesting * strings randomly in Java

I use ScalaCheck for automatic unit testing. Its default String generator (that is, its default Arbitrary[String] instance) is a little powerful too , usually creating an unreadable mess consisting mostly of characters that I am not trying to support, and my system cannot even do.

I decided to create several instances of Arbitrary[String] , and I'm trying to figure out what is there. Here are a few examples of String classes that would be useful for testing:

  • core multilingual string strings
  • astral strings
  • latin strings (including extensions a / b)
  • French words
  • lines from left to right Language lines
  • from right to left
  • Chinese offers
  • Web Strings (strings taken from a character set of 99.9999% of web content).
  • use your imagination ...

Are there libraries that can make these or similar strings randomly?

+4
source share
2 answers

I would choose a different approach. All of your examples are mapped to character blocks in Unicode. See http://www.fileformat.info/info/unicode/block/index.htm Just select the blocks you need and then create random strings that are limited to these ranges.

 int count = 10; StringBuilder out = new StringBuilder(); Random rand = new Random(0); for (int i = 0; i < count; i++) { char ch = rand.nextInt(numCharsInRange) + firstCharInRange; out.append(ch); } return out.toString(); 

Another approach would be to capture random fragments of pre-arranged text from different languages. You can grab some of them here: http://www.unicode.org/standard/WhatIsUnicode.html Just look at the translations.

+1
source

Try

 RandomStringUtils.random(10, true, false) 

The parameters are as follows: int count, boolean letters, boolean numbers

You will need to import org.apache.commons.lang.RandomStringUtils;

0
source

All Articles