0

For testing purposes, you may want to supply random input to a method, but Alphanumeric generators like scala.util.Random.alphanumeric don't cover the full possible input in many cases.

What is the best way to generate random unicode strings of a given length?

0

2 Answers 2

3

It can be done without mutable buffers or variables.

import java.lang.Character
import util.Random

def randomUnicodeString(length: Int) =
  LazyList.continually(Random.nextInt(0xFFFF))
          .collect{case c if Character.isDefined(c) => c.toChar}
          .take(length)
          .mkString
Sign up to request clarification or add additional context in comments.

1 Comment

In Scala<2.13 you must replace LazyList with Stream
3

Consider ScalaCheck generators, for example

import org.scalatest._
import org.scalatestplus.scalacheck.ScalaCheckDrivenPropertyChecks
import org.scalacheck.{Arbitrary, Gen}

class AlphanumericGeneratorSpec extends FlatSpec with Matchers with ScalaCheckDrivenPropertyChecks {
  "Arbitrary.arbString" should "generate arbitrary strings" in {
    forAll(Arbitrary.arbString.arbitrary) { s =>
      println(s)
    }
  }
}

outputs something like

⦍鸟㗴鲪ᬎ㺥
䊪仍ୁ㕃댊벝蛎瓊倊錾釨㚺㥌ੜ
仁벵ຝ洱霷쀙쵟㹳灢瑤쌴
詢蓫䑽ᵋ앹䦆딂෡ᄬ鉖
醃䦼컠᳞遱掊醌涰ᚕ苇泟Ễ㒪뭩綗ᄍ䉱屮藓䦆䜜㴹煉ں㸲䛊ᓠ셴켶䦖垯ᡤ卆婱羘畕⁋䢅ﯫ訆݊ⵕ건
썁㙁䠼臩蚁䬣ݍ⓸⤵犕꺭ཛྷ잁闓襥鯽啌ꟴ翏㫺ܪሷ뢛快腊机ᰵ蟽ﳥɴ೑馣䪚軇鿾㹄Ꝓ㿈뿭땧颟ᜒ纈ꔧ毚뒢兊屼
矵귳옘䕌Ⱌ`凣쐕튌ằ惿獄ꍝ䫗끒뮌錑꓌㶩ዒ絽໸鹣煋襁ꨔᔵ팋屜姙ꙇᡛ뾗꘎焑酸ಹ乵텣쳊疟䉴﬈투꿠㱎ᅧ텎ᑽﹽ跡
橛櫯ꆠ葲逊ᵈ엦災盨捻棉䟔縆⦽㯠侄ᙑד볓끀謹翆滳ץ픎Ꝛ餮范샥➮⊠㇔鐿詥ꊔ텦䟑徜ꮉ绳䜁빎학⮴䑢悋铃揍挛⭂장踛ﻥ螴阶ퟠ햇
ᶭ䯵꧓擃轤ꦧꦛ

To set fixed size try

def arbUnicodeGen(size: Int) = 
  Gen.listOfN(size, Arbitrary.arbChar.arbitrary).map(_.mkString)

and then

  "arbUnicodeGen" should "generate fixed length arbitrary strings" in {
    forAll(arbUnicodeGen(10)) { s =>
      println(s)
    }
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.