diff options
author | michelou <michelou@epfl.ch> | 2007-07-24 17:30:23 +0000 |
---|---|---|
committer | michelou <michelou@epfl.ch> | 2007-07-24 17:30:23 +0000 |
commit | d8504784b821fb64f411adf7c551ee4646e5f99c (patch) | |
tree | 73534241fb12d0b55970d9addc319dde9ebd6048 | |
parent | 551e31ec7d1f3399cb1a0193f68268f292e96699 (diff) | |
download | scala-d8504784b821fb64f411adf7c551ee4646e5f99c.tar.gz scala-d8504784b821fb64f411adf7c551ee4646e5f99c.tar.bz2 scala-d8504784b821fb64f411adf7c551ee4646e5f99c.zip |
added class scala.Random
-rw-r--r-- | src/actors/scala/actors/remote/TcpService.scala | 16 | ||||
-rw-r--r-- | src/dotnet-library/scala/Random.scala | 74 | ||||
-rw-r--r-- | src/library/scala/BigInt.scala | 7 | ||||
-rw-r--r-- | src/library/scala/Random.scala | 72 | ||||
-rwxr-xr-x | test/scalatest | 2 |
5 files changed, 157 insertions, 14 deletions
diff --git a/src/actors/scala/actors/remote/TcpService.scala b/src/actors/scala/actors/remote/TcpService.scala index e40384749b..d9181f96b6 100644 --- a/src/actors/scala/actors/remote/TcpService.scala +++ b/src/actors/scala/actors/remote/TcpService.scala @@ -16,8 +16,6 @@ import java.lang.{Thread, SecurityException} import java.io.{DataInputStream, DataOutputStream, IOException} import java.net.{InetAddress, ServerSocket, Socket, UnknownHostException} -import compat.Platform - import scala.collection.mutable.HashMap /* Object TcpService. @@ -26,9 +24,9 @@ import scala.collection.mutable.HashMap * @author Philipp Haller */ object TcpService { - val random = new java.util.Random(Platform.currentTime) + val random = new Random - def generatePort: int = { + def generatePort: Int = { var portnum = 0 try { portnum = 8000 + random.nextInt(500) @@ -47,7 +45,7 @@ object TcpService { portnum } - var BufSize: int = 65536 + var BufSize: Int = 65536 } /* Class TcpService. @@ -61,14 +59,14 @@ class TcpService(port: Int) extends Thread with Service { private val internalNode = new Node(InetAddress.getLocalHost().getHostAddress(), port) def node: Node = internalNode - private val pendingSends = new HashMap[Node, List[Array[byte]]] + private val pendingSends = new HashMap[Node, List[Array[Byte]]] /** * Sends a byte array to another node on the network. * If the node is not yet up, up to <code>TcpService.BufSize</code> * messages are buffered. */ - def send(node: Node, data: Array[byte]): Unit = synchronized { + def send(node: Node, data: Array[Byte]): Unit = synchronized { def bufferMsg(t: Throwable) = { // buffer message, so that it can be re-sent @@ -171,7 +169,7 @@ class TcpService(port: Int) extends Thread with Service { } } - def isReachable(node: Node): boolean = + def isReachable(node: Node): Boolean = if (isConnected(node)) true else try { connect(node) @@ -212,7 +210,7 @@ class TcpServiceWorker(parent: TcpService, so: Socket) extends Thread { } } - def transmit(data: Array[byte]): Unit = synchronized { + def transmit(data: Array[Byte]): Unit = synchronized { Debug.info(this+": transmitting data...") dataout.writeInt(data.length) dataout.write(data) diff --git a/src/dotnet-library/scala/Random.scala b/src/dotnet-library/scala/Random.scala new file mode 100644 index 0000000000..45e3d6619d --- /dev/null +++ b/src/dotnet-library/scala/Random.scala @@ -0,0 +1,74 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: $ + + +package scala + +/** + * @author Stephane Micheloud + */ +class Random(self0: System.Random) { + private var rnd = self0 // see setSeed(seed) + def self = rnd + + /** Creates a new random number generator using a single long seed. */ + def this(seed: Long) = this(new System.Random(seed.toInt)) + + /** Creates a new random number generator using a single integer seed. */ + def this(seed: Int) = this(new System.Random(seed)) + + /** Creates a new random number generator. */ + def this() = this(Math.abs(System.Environment.TickCount)) + + /** Returns the next pseudorandom, uniformly distributed boolean value + * from this random number generator's sequence. + */ + def nextBoolean(): Boolean = (nextInt() & 0x1) == 1 + + /** Generates random bytes and places them into a user-supplied byte + * array. + */ + def nextBytes(bytes: Array[Byte]) { rnd.NextBytes(bytes) } + + /** Returns the next pseudorandom, uniformly distributed double value + * between 0.0 and 1.0 from this random number generator's sequence. + */ + def nextDouble(): Double = rnd.NextDouble() + + /** Returns the next pseudorandom, uniformly distributed float value + * between 0.0 and 1.0 from this random number generator's sequence. + */ + def nextFloat(): Float = nextDouble().toFloat + + /** Returns the next pseudorandom, Gaussian ("normally") distributed + * double value with mean 0.0 and standard deviation 1.0 from this + * random number generator's sequence. + */ + //def nextGaussian(): Double + + /** Returns the next pseudorandom, uniformly distributed int value + * from this random number generator's sequence. + */ + def nextInt(): Int = rnd.Next() + + /** Returns a pseudorandom, uniformly distributed int value between 0 + * (inclusive) and the specified value (exclusive), drawn from this + * random number generator's sequence. + */ + def nextInt(n: Int): Int = rnd.Next(0, n) + + /** Returns the next pseudorandom, uniformly distributed long value + * from this random number generator's sequence. + */ + def nextLong(): Long = nextInt().toLong // 2x nextInt() ?! + + def setSeed(seed: Long) { rnd = new System.Random(seed.toInt) } + +} diff --git a/src/library/scala/BigInt.scala b/src/library/scala/BigInt.scala index 0df92ec0ec..a24d141aec 100644 --- a/src/library/scala/BigInt.scala +++ b/src/library/scala/BigInt.scala @@ -11,7 +11,6 @@ package scala import java.math.BigInteger -import java.util.Random /** * @author Martin Odersky @@ -62,7 +61,7 @@ object BigInt { * with the specified bitLength. */ def apply(bitlength: Int, certaInty: Int, rnd: Random): BigInt = - new BigInt(new BigInteger(bitlength, certaInty, rnd)) + new BigInt(new BigInteger(bitlength, certaInty, rnd.self)) /** Constructs a randomly generated BigInt, uniformly distributed over the * range 0 to (2 ^ numBits - 1), inclusive. @@ -72,7 +71,7 @@ object BigInt { * @return ... */ def apply(numbits: Int, rnd: Random): BigInt = - new BigInt(new BigInteger(numbits, rnd)) + new BigInt(new BigInteger(numbits, rnd.self)) /** Translates the decimal String representation of a BigInt into a BigInt. */ @@ -92,7 +91,7 @@ object BigInt { /** Returns a positive BigInt that is probably prime, with the specified bitLength. */ def probablePrime(bitLength: Int, rnd: Random): BigInt = - new BigInt(BigInteger.probablePrime(bitLength, rnd)) + new BigInt(BigInteger.probablePrime(bitLength, rnd.self)) /** Implicit conversion from <code>int</code> to <code>BigInt</code>. */ diff --git a/src/library/scala/Random.scala b/src/library/scala/Random.scala new file mode 100644 index 0000000000..6cc0d51ada --- /dev/null +++ b/src/library/scala/Random.scala @@ -0,0 +1,72 @@ +/* __ *\ +** ________ ___ / / ___ Scala API ** +** / __/ __// _ | / / / _ | (c) 2006-2007, LAMP/EPFL ** +** __\ \/ /__/ __ |/ /__/ __ | http://scala-lang.org/ ** +** /____/\___/_/ |_/____/_/ | | ** +** |/ ** +\* */ + +// $Id: $ + + +package scala + +/** + * @author Stephane Micheloud + */ +class Random(val self: java.util.Random) { + + /** Creates a new random number generator using a single long seed. */ + def this(seed: Long) = this(new java.util.Random(seed)) + + /** Creates a new random number generator using a single integer seed. */ + def this(seed: Int) = this(seed.toLong) + + /** Creates a new random number generator. */ + def this() = this(compat.Platform.currentTime) + + /** Returns the next pseudorandom, uniformly distributed boolean value + * from this random number generator's sequence. + */ + def nextBoolean(): Boolean = self.nextBoolean() + + /** Generates random bytes and places them into a user-supplied byte + * array. + */ + def nextBytes(bytes: Array[Byte]) { self.nextBytes(bytes) } + + /** Returns the next pseudorandom, uniformly distributed double value + * between 0.0 and 1.0 from this random number generator's sequence. + */ + def nextDouble(): Double = self.nextDouble() + + /** Returns the next pseudorandom, uniformly distributed float value + * between 0.0 and 1.0 from this random number generator's sequence. + */ + def nextFloat(): Float = self.nextFloat() + + /** Returns the next pseudorandom, Gaussian ("normally") distributed + * double value with mean 0.0 and standard deviation 1.0 from this + * random number generator's sequence. + */ + //def nextGaussian(): Double = self.nextGaussian() + + /** Returns the next pseudorandom, uniformly distributed int value + * from this random number generator's sequence. + */ + def nextInt(): Int = self.nextInt() + + /** Returns a pseudorandom, uniformly distributed int value between 0 + * (inclusive) and the specified value (exclusive), drawn from this + * random number generator's sequence. + */ + def nextInt(n: Int): Int = self.nextInt(n) + + /** Returns the next pseudorandom, uniformly distributed long value + * from this random number generator's sequence. + */ + def nextLong(): Long = self.nextLong() + + def setSeed(seed: Long) { self.setSeed(seed) } + +} diff --git a/test/scalatest b/test/scalatest index 14f8f38584..d1f4557df3 100755 --- a/test/scalatest +++ b/test/scalatest @@ -295,7 +295,7 @@ test_run_msil() { rm -f "$dstbase".$MSIL && rm -f "$dstbase".$EXE && - $SCALAC -nowarn -target:msil -o "$os_dstbase" -r $assemblies "$@" \ + $SCALAC -nowarn -target:msil -Xassem "$os_dstbase" -Xassem-path $assemblies "$@" \ -sourcepath "$PREFIX/build/msil/src" "$os_srcbase".scala && case "$UNAME" in CYGWIN* ) |