-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A pseudorandom number generator. (#56)
Introduces a new utility class `cgsuite.util.Random` that provides a pseudorandom number source.
- Loading branch information
1 parent
827d8c4
commit 8bb0916
Showing
8 changed files
with
93 additions
and
7 deletions.
There are no files selected for viewing
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
34 changes: 34 additions & 0 deletions
34
lib/core/src/main/resources/org/cgsuite/lang/resources/cgsuite/util/Random.cgs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/*${cgsuite.banner}*/ | ||
|
||
/** A pseudorandom number generator. | ||
* | ||
* To construct an instance of `Random`, specify an integer seed: | ||
* | ||
* \display{random := Random(1474)} | ||
* | ||
* Then use the [[#NextInteger]] method to generate output. This example produces integers `n` with `0 <= n < 100`: | ||
* | ||
* \display{random.NextInteger(100)} | ||
* | ||
* \display{[random.NextInteger(100) for n from 1 to 20]} | ||
*/ | ||
mutable system class Random( | ||
/** The seed for this `Random`. The seed must satisfy: | ||
* | ||
* `-2^63 <= seed < 2^63` | ||
*/ | ||
seed as Integer | ||
) | ||
|
||
/** The next `Integer` in this stream of pseudorandom numbers. The output `n` of `NextInteger(upperBound)` will always | ||
* satisfy | ||
* | ||
* `0 <= n < upperBound` | ||
* | ||
* and will be (pseudo-)uniformly distributed within that range. | ||
*/ | ||
external def NextInteger(upperBound as Integer); | ||
|
||
override def ToOutput := "Random(" + seed.ToOutput + ")"; | ||
|
||
end |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.cgsuite.util | ||
|
||
import org.cgsuite.core.Integer | ||
import org.cgsuite.core.Values.one | ||
import org.cgsuite.exception.EvalException | ||
|
||
class Random(val seed: Integer) { | ||
|
||
private val random = new scala.util.Random(seed.longValue) | ||
|
||
def nextInteger(upperBound: Integer): Integer = { | ||
if (upperBound < one) { | ||
throw EvalException("Upper bound for `NextInteger` must be >= 1.") | ||
} | ||
val bits = (upperBound - one).lb.intValue + 1 | ||
var result: Integer = Integer(BigInt(bits, random)) | ||
while (result >= upperBound) { | ||
result = Integer(BigInt(bits, random)) | ||
} | ||
result | ||
} | ||
|
||
} |
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
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