Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to generate random string without repetition in Scala

1 Answer

0 votes
import scala.util.Random

object UniqueRandomStringGenerator {
  def generateUniqueRandomString(total: Int): String = {
    val chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
    val usedChars = scala.collection.mutable.Set[Char]()
    val result = new StringBuilder

    while (result.length < total) {
      val randomIndex = Random.nextInt(chars.length)
      val randomChar = chars(randomIndex)
      if (!usedChars.contains(randomChar)) {
        result.append(randomChar)
        usedChars.add(randomChar)
      }
    }

    result.toString()
  }

  def main(args: Array[String]): Unit = {
    println(generateUniqueRandomString(15))
  }
}




/*
run:
  
HqL9jVg2ZlWcGO4
  
*/

 



answered Nov 5, 2024 by avibootz

Related questions

...