Skip to content

Commit d4d533d

Browse files
author
Sean Friedowitz
authored
Merge pull request #11 from CitrineInformatics/none-constructors
Add support for Option[Long]
2 parents 98fd913 + 3d32de6 commit d4d533d

File tree

4 files changed

+45
-16
lines changed

4 files changed

+45
-16
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: scala
22
scala:
3-
- 2.13.4
3+
- 2.13.11
44
jdk: openjdk11
55

66
before_install:

build.sbt

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import Dependencies._
22

3-
ThisBuild / scalaVersion := "2.13.4"
3+
ThisBuild / scalaVersion := "2.13.11"
44
ThisBuild / organization := "io.citrine"
55
ThisBuild / organizationName := "Citrine Informatics"
66
ThisBuild / homepage := Some(url("https://github.com/CitrineInformatics/sprandom"))
7-
ThisBuild / developers := List(Developer(
8-
id="Citrine",
9-
name="Citrine Informatics",
10-
11-
url=url("https://github.com/CitrineInformatics")
12-
))
7+
ThisBuild / developers := List(
8+
Developer(
9+
id = "Citrine",
10+
name = "Citrine Informatics",
11+
email = "[email protected]",
12+
url = url("https://github.com/CitrineInformatics")
13+
)
14+
)
1315
ThisBuild / description := "Splittable, serializable pseudorandom number generation in Scala."
1416
ThisBuild / licenses += "Apache-2.0" -> url("http://www.apache.org/licenses/LICENSE-2.0.txt")
1517

src/main/scala/io/citrine/random/Random.scala

+18-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import scala.collection.BuildFrom
55
import scala.collection.mutable.ArrayBuffer
66

77
class Random private (seed: Random.RandomSeed) extends Serializable {
8+
89
@transient private lazy val baseRng: SplittableRandom = seed match {
910
case Random.EmptySeed() => new SplittableRandom
10-
case Random.IntSeed(value) => new SplittableRandom(value.toLong)
1111
case Random.LongSeed(value) => new SplittableRandom(value)
1212
case Random.RngSeed(value) => value.baseRng.split()
1313
}
@@ -163,17 +163,27 @@ class Random private (seed: Random.RandomSeed) extends Serializable {
163163
}
164164

165165
object Random {
166-
sealed trait RandomSeed
167-
case class EmptySeed() extends RandomSeed
168-
case class IntSeed(value: Int) extends RandomSeed
169-
case class LongSeed(value: Long) extends RandomSeed
170-
case class RngSeed(value: Random) extends RandomSeed
166+
167+
sealed private trait RandomSeed
168+
private case class EmptySeed() extends RandomSeed
169+
private case class LongSeed(value: Long) extends RandomSeed
170+
private case class RngSeed(value: Random) extends RandomSeed
171171

172172
def apply(): Random = new Random(EmptySeed())
173-
def apply(seed: Int): Random = new Random(IntSeed(seed))
174-
def apply(seed: Long): Random = new Random(LongSeed(seed))
173+
175174
def apply(seed: Random): Random = new Random(RngSeed(seed))
176175

176+
def apply(seed: Int): Random = new Random(LongSeed(seed.toLong))
177+
178+
def apply(seed: Long): Random = new Random(LongSeed(seed))
179+
180+
def apply(seed: Option[Long]): Random = {
181+
seed match {
182+
case Some(x) => new Random(LongSeed(x))
183+
case _ => new Random(EmptySeed())
184+
}
185+
}
186+
177187
/** Construct a default Random object seeded from the global RNG. */
178188
def default: Random = Random(scala.util.Random.nextLong())
179189

src/test/scala/io/citrine/random/RandomTest.scala

+17
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,21 @@ class RandomTest extends AnyFunSuite {
211211
val firstResult = allResults.head
212212
allResults.tail.foreach(thisResult => assert(thisResult == firstResult))
213213
}
214+
215+
test("Random can be seeded with optional values") {
216+
val seed: Long = 53
217+
val optionSeed: Option[Long] = Some(seed)
218+
val emptySeed: Option[Long] = None
219+
220+
val direct = Random(seed)
221+
val indirect = Random(optionSeed)
222+
val empty = Random(emptySeed)
223+
224+
val directValue = direct.nextLong()
225+
val indirectValue = indirect.nextLong()
226+
val emptyValue = empty.nextLong()
227+
228+
assert(directValue == indirectValue)
229+
assert(directValue != emptyValue)
230+
}
214231
}

0 commit comments

Comments
 (0)