Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3990600
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:30:54+00:00 2026-05-20T06:30:54+00:00

I have some generators like this: val fooRepr = oneOf(a, b, c, d, e)

  • 0

I have some generators like this:

val fooRepr = oneOf(a, b, c, d, e)
val foo = for (s <- choose(1, 5); c <- listOfN(s, fooRepr)) yield c.mkString("$")

This leads to duplicates … I might get two a’s, etc. What I really want is to generate random permutation with exactly 0 or 1 or each of a, b, c, d, or e (with at least one of something), in any order.

I was thinking there must be an easy way, but I’m struggling to even find a hard way. 🙂

Edited: Ok, this seems to work:

val foo = for (s <- choose(1, 5);
               c <- permute(s, a, b, c, d, e)) yield c.mkString("$")

def permute[T](n: Int, gs: Gen[T]*): Gen[Seq[T]] = {
  val perm = Random.shuffle(gs.toList)
  for {
    is <- pick(n, 1 until gs.size)
    xs <- sequence[List,T](is.toList.map(perm(_)))
  } yield xs
}

…borrowing heavily from Gen.pick.

Thanks for your help, -Eric

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:30:54+00:00Added an answer on May 20, 2026 at 6:30 am

    Rex, thanks for clarifying exactly what I’m trying to do, and that’s useful code, but perhaps not so nice with scalacheck, particularly if the generators in question are quite complex. In my particular case the generators a, b, c, etc. are generating huge strings.

    Anyhow, there was a bug in my solution above; what worked for me is below. I put a tiny project demonstrating how to do this at github

    The guts of it is below. If there’s a better way, I’d love to know it…

    package powerset
    
    import org.scalacheck._
    import org.scalacheck.Gen._
    import org.scalacheck.Gen
    import scala.util.Random
    
    object PowersetPermutations extends Properties("PowersetPermutations") {
    
      def a: Gen[String] = value("a")
    
      def b: Gen[String] = value("b")
    
      def c: Gen[String] = value("c")
    
      def d: Gen[String] = value("d")
    
      def e: Gen[String] = value("e")
    
      val foo = for (s <- choose(1, 5);
                     c <- permute(s, a, b, c, d, e)) yield c.mkString
    
      def permute[T](n: Int, gs: Gen[T]*): Gen[Seq[T]] = {
        val perm = Random.shuffle(gs.toList)
        for {
          is <- pick(n, 0 until gs.size)
          xs <- sequence[List, T](is.toList.map(perm(_)))
        } yield xs
      }
    
      implicit def arbString: Arbitrary[String] = Arbitrary(foo)
    
      property("powerset") = Prop.forAll {
        a: String => println(a); true
      }
    }
    

    Thanks,
    Eric

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some LINQ code that generates a list of strings, like this: var
I have some PHP code which looks roughly like this require_once 'cache.php'; require_once 'generator.php';
I have some C# code that generates google maps. This codes looks at all
I have a few infinite generator methods, including some long-running and infinitely-long-running generators. IEnumerable<T>
Good day, I have a hibernate mapping which goes something like this <class name=Person>
I have an entity that is mapped with a <join> element like this: <class
I would like to have something like this be generated from hbm2ddl: ______________ ______________
I have a mapping like this <class name=UserFileSummary table=UserFile lazy=false> <id name=FileId column=UserFileId type=int>
I have some code: @Id @SequenceGenerator(name = SOMETHING_SEQ) @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = SOMETHING_SEQ)
I have some code that generates image of a pie chart. It's a general

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.