This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Re: scala.util.Random

8 replies
David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.


On Fri, Nov 20, 2009 at 10:41 AM, martin odersky <martin.odersky@epfl.ch> wrote:
Have people followed the long discussion about the bug in
scala.util.Random at scala-users?
That had me thinking (not for the first time!) why does Scala do a
Random at all? Is it exactly the same as java.util.Random, or is there
some added value? If it is exactly the same, we should remove it, and
maybe add type alias to the scala.util package object.

Was it in Scala for MSIL compatibility?
 

Cheers

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
scala.util.Random

Have people followed the long discussion about the bug in
scala.util.Random at scala-users?
That had me thinking (not for the first time!) why does Scala do a
Random at all? Is it exactly the same as java.util.Random, or is there
some added value? If it is exactly the same, we should remove it, and
maybe add type alias to the scala.util package object.

Cheers

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: scala.util.Random

On Fri, Nov 20, 2009 at 07:41:57PM +0100, martin odersky wrote:
> Have people followed the long discussion about the bug in
> scala.util.Random at scala-users? That had me thinking (not for the
> first time!) why does Scala do a Random at all? Is it exactly the same
> as java.util.Random, or is there some added value? If it is exactly
> the same, we should remove it, and maybe add type alias to the
> scala.util package object.

What I find useful more than the class is the object, so for the 99.9%
of the time that I don't need some kind of awesome randomness but simply
an arbitrary value, it's easily available via Random.nextInt() or
whatever. And that object is also where I put shuffle.

def shuffle[T, CC[X] <: Traversable[X]](coll: CC[T])(implicit bf: CanBuildFrom[CC[T], T, CC[T]]): CC[T]

In addition I did at some point give it a couple handy functions like
nextString() which I used in io.Path when I needed a randomly generated
file or dir name.

On the whole I'd like to keep it.

David Hall 4
Joined: 2009-08-21,
User offline. Last seen 42 years 45 weeks ago.
Re: scala.util.Random

On Fri, Nov 20, 2009 at 10:41 AM, martin odersky wrote:
> Have people followed the long discussion about the bug in
> scala.util.Random at scala-users?
> That had me thinking (not for the first time!) why does Scala do a
> Random at all? Is it exactly the same as java.util.Random, or is there
> some added value? If it is exactly the same, we should remove it, and
> maybe add type alias to the scala.util package object.
>
> Cheers
>
>  -- Martin
>

FWIW, I have a monadic Rand trait and accompanying companion objects
that supports foreach/filter/map/etc. It's geared towards sampling
from probability distributions, and it's currently backed by
java.util.Random.///

It needs some cleanup (specifically the ability to use a seed), but
I'd be happy to contribute it.

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: scala.util.Random

And quoting you from 18 months ago, in response to an email thread
where I basically asked the same question you're asking now:

"I did not realize there was a Random class in the scala package. I
agree it does not belong there. One might discuss whether or not a
Scala-specific Random is needed, but if we decide we need it it should
be implemented to the highest standards, and should be put in
scala.util.

Cheers

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: scala.util.Random

On Fri, Nov 20, 2009 at 10:05 PM, Ricky Clarkson
wrote:
> And quoting you from 18 months ago, in response to an email thread
> where I basically asked the same question you're asking now:
>
> "I did not realize there was a Random class in the scala package. I
> agree it does not belong there. One might discuss whether or not a
> Scala-specific Random is needed, but if we decide we need it it should
> be implemented to the highest standards, and should be put in
> scala.util.
>
> Cheers
>
>  -- Martin"
>
> I would guess only the move from scala to scala.util was implemented.
>
Thanks for reminding me. There are too many concurrent threads going
on for me to remember all of them!

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: scala.util.Random
I'd like it, if it were Traversable. Being able to do "new Rand take 10" or "new Rand takeWhile predicate" would be nice. Add some control for the domain and distribution, and I'd be set.
On Fri, Nov 20, 2009 at 5:30 PM, David Hall <dlwh@cs.berkeley.edu> wrote:
On Fri, Nov 20, 2009 at 10:41 AM, martin odersky <martin.odersky@epfl.ch> wrote:
> Have people followed the long discussion about the bug in
> scala.util.Random at scala-users?
> That had me thinking (not for the first time!) why does Scala do a
> Random at all? Is it exactly the same as java.util.Random, or is there
> some added value? If it is exactly the same, we should remove it, and
> maybe add type alias to the scala.util package object.
>
> Cheers
>
>  -- Martin
>

FWIW, I have a monadic Rand trait and accompanying companion objects
that supports foreach/filter/map/etc. It's geared towards sampling
from probability distributions, and it's currently backed by
java.util.Random.///

It needs some cleanup (specifically the ability to use a seed), but
I'd be happy to contribute it.

-- David

P.S. The trait is basically:

trait Rand[+T] { outer : Rand[T] =>
 def draw() : T
 def get() = draw;

 /**
 * Return a Seq of n samples
 */
 def sample(n : Int): Seq[T] = List.tabulate(n)(x => get);

 /**
* An infinitely long iterator that samples repeatedly from the Rand
* @return an iterator that repeatedly samples
*/
 def samples:Iterator[T] = new Iterator[T] {
   def hasNext() = true;
   def next() = get();
 }

 /**
* Converts a random sampler of one type to a random sampler of another type.
* Examples:
* randInt(10).flatMap(x => randInt(3 * x.asInstanceOf[Int]) gives a
Rand[Int] in the range [0,30]
* Equivalently, for(x &lt;- randInt(10); y &lt;- randInt(30 *x)) yield y;
*
* @param f the transform to apply to the sampled value.
*
*/
 def flatMap[E](f : T => Rand[E] ) = {
   new Rand[E] {
     def draw = f(outer.get).get
   }
 }

 /**
* Converts a random sampler of one type to a random sampler of another type.
* Examples:
* uniform.map(_*2) gives a Rand[Double] in the range [0,2]
* Equivalently, for(x &lt;- uniform) yield 2*x;
*
* @param f the transform to apply to the sampled value.
*
*/
 def map[E](f : T=>E) = {
   new Rand[E] {
     def draw = f(outer.get);
   }
 }

 /**
* Samples one element and qpplies the provided function to it.
* Despite the name, the function is applied once. Sample usage:
* <pre> for(x &lt;- Rand.uniform) { println(x) } </pre>
*
* @param f the function to be applied
*/
 def foreach(f : T=>Unit) = f(get);

 def filter(p: T=>Boolean) = condition(p);

 def condition(p : T => Boolean) = new Rand[T] {
   def draw() = {
     var x = outer.get;
     while(!p(x)) {
       x = outer.get;
     }
     x
   }
 }

}



--
Daniel C. Sobral

Veni, vidi, veterni.
Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: scala.util.Random

replicateM and filterM of Scalaz.

Daniel Sobral wrote:
> I'd like it, if it were Traversable. Being able to do "new Rand take
> 10" or "new Rand takeWhile predicate" would be nice. Add some control
> for the domain and distribution, and I'd be set.
> On Fri, Nov 20, 2009 at 5:30 PM, David Hall > wrote:
>
> On Fri, Nov 20, 2009 at 10:41 AM, martin odersky
> > wrote:
> > Have people followed the long discussion about the bug in
> > scala.util.Random at scala-users?
> > That had me thinking (not for the first time!) why does Scala do a
> > Random at all? Is it exactly the same as java.util.Random, or is
> there
> > some added value? If it is exactly the same, we should remove
> it, and
> > maybe add type alias to the scala.util package object.
> >
> > Cheers
> >
> > -- Martin
> >
>
> FWIW, I have a monadic Rand trait and accompanying companion objects
> that supports foreach/filter/map/etc. It's geared towards sampling
> from probability distributions, and it's currently backed by
> java.util.Random.///
>
> It needs some cleanup (specifically the ability to use a seed), but
> I'd be happy to contribute it.
>
> -- David
>
> P.S. The trait is basically:
>
> trait Rand[+T] { outer : Rand[T] =>
> def draw() : T
> def get() = draw;
>
> /**
> * Return a Seq of n samples
> */
> def sample(n : Int): Seq[T] = List.tabulate(n)(x => get);
>
> /**
> * An infinitely long iterator that samples repeatedly from the Rand
> * @return an iterator that repeatedly samples
> */
> def samples:Iterator[T] = new Iterator[T] {
> def hasNext() = true;
> def next() = get();
> }
>
> /**
> * Converts a random sampler of one type to a random sampler of
> another type.
> * Examples:
> * randInt(10).flatMap(x => randInt(3 * x.asInstanceOf[Int]) gives a
> Rand[Int] in the range [0,30]
> * Equivalently, for(x <- randInt(10); y <- randInt(30 *x))
> yield y;
> *
> * @param f the transform to apply to the sampled value.
> *
> */
> def flatMap[E](f : T => Rand[E] ) = {
> new Rand[E] {
> def draw = f(outer.get).get
> }
> }
>
> /**
> * Converts a random sampler of one type to a random sampler of
> another type.
> * Examples:
> * uniform.map(_*2) gives a Rand[Double] in the range [0,2]
> * Equivalently, for(x <- uniform) yield 2*x;
> *
> * @param f the transform to apply to the sampled value.
> *
> */
> def map[E](f : T=>E) = {
> new Rand[E] {
> def draw = f(outer.get);
> }
> }
>
> /**
> * Samples one element and qpplies the provided function to it.
> * Despite the name, the function is applied once. Sample usage:
> *

 for(x <- Rand.uniform) { println(x) } 

> *
> * @param f the function to be applied
> */
> def foreach(f : T=>Unit) = f(get);
>
> def filter(p: T=>Boolean) = condition(p);
>
> def condition(p : T => Boolean) = new Rand[T] {
> def draw() = {
> var x = outer.get;
> while(!p(x)) {
> x = outer.get;
> }
> x
> }
> }
>
> }
>
>
>
>

Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.
Re: scala.util.Random

>>>>> "Daniel" == Daniel Sobral writes:

Daniel> I'd like it, if it were Traversable. Being able to do "new Rand
Daniel> take 10" or "new Rand takeWhile predicate"

there's this:

scala> new util.Random
res0: scala.util.Random = scala.util.Random@36029483

scala> Stream.continually(res0.nextInt).take(10).force
res1: scala.collection.immutable.Stream[Int] = Stream(-1936760804, 462907986, -222459505, -1957085825, -255278920, -1674168790, -686570258, -1396096595, -1750024454, 1666166992)

or if you're squeamish about Stream:

scala> Iterator.continually(res0.nextInt).take(10).toList
res2: List[Int] = List(2071322280, -957252, -108660259, 1637398716, 140269210, -1484847171, 1748170963, -1606268246, -1433489913, 1770960146)

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland