Last active
May 25, 2024 10:18
-
-
Save dacr/8e025d8fa0bc55d351b0092051c7f86f to your computer and use it in GitHub Desktop.
generate a random loto play / published by https://github.com/dacr/code-examples-manager #ff0c5b51-591f-4fd1-b97d-b619d6c5d2c1/bf6e9c74cb77a1e01c6ede5223007ae95bf5d72d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// summary : generate a random loto play | |
// keywords : scala, loto, random, rng, fdj, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : ff0c5b51-591f-4fd1-b97d-b619d6c5d2c1 | |
// created-on : 2021-02-10T21:45:49Z | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : scala-cli $file | |
// --------------------- | |
//> using scala "3.4.2" | |
// --------------------- | |
def gen(limit:Int):Int = scala.util.Random.nextInt(limit)+1 | |
def select(limit:Int,count:Int):List[Int]= { | |
LazyList | |
.continually(gen(limit)) | |
.scanLeft(Set.empty[Int]) ( (selected, randomlyChosen) => selected+randomlyChosen) | |
.find(_.size == count) | |
.get | |
.toList | |
.sorted | |
} | |
println("nums : " + select(49,5).mkString(" - ")) | |
println("stars : " + select(10,1).mkString(" - ")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment