Github user mateiz commented on a diff in the pull request: https://github.com/apache/spark/pull/1520#discussion_r15215522 --- Diff: mllib/src/main/scala/org/apache/spark/mllib/random/DistributionGenerator.scala --- @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.mllib.random + +import cern.jet.random.Poisson +import cern.jet.random.engine.DRand + +import org.apache.spark.util.random.{XORShiftRandom, Pseudorandom} + +/** + * Trait for random number generators that generate i.i.d values from a distribution. + */ +trait DistributionGenerator extends Pseudorandom with Serializable { + + /** + * @return An i.i.d sample as a Double from an underlying distribution. + */ + def nextValue(): Double + + /** + * @return A copy of the DistributionGenerator with a new instance of the rng object used in the + * class when applicable. Each partition has a unique seed and therefore requires its + * own instance of the DistributionGenerator. + */ + def newInstance(): DistributionGenerator +} + +/** + * Generates i.i.d. samples from U[0.0, 1.0] + */ +class UniformGenerator() extends DistributionGenerator { + + // XORShiftRandom for better performance. Thread safety isn't necessary here. + private val random = new XORShiftRandom() + + /** + * @return An i.i.d sample as a Double from U[0.0, 1.0]. + */ + override def nextValue(): Double = { + random.nextDouble() + } + + /** Set random seed. */ + override def setSeed(seed: Long) = random.setSeed(seed) + + override def newInstance(): UniformGenerator = new UniformGenerator() +} + +/** + * Generates i.i.d. samples from the Standard Normal Distribution. + */ +class StandardNormalGenerator() extends DistributionGenerator { + + // XORShiftRandom for better performance. Thread safety isn't necessary here. + private val random = new XORShiftRandom() --- End diff -- Is it allowed to use a DistributionGenerator before calling setSeed? It would seem simpler to disallow that, but it seems to be something it got from trait Pseudorandom.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---