srowen commented on a change in pull request #31535: URL: https://github.com/apache/spark/pull/31535#discussion_r580310114
########## File path: mllib/src/main/scala/org/apache/spark/ml/tuning/ParamRandomBuilder.scala ########## @@ -0,0 +1,160 @@ +/* + * 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.ml.tuning + +import org.apache.spark.annotation.Since +import org.apache.spark.ml.param._ +import org.apache.spark.ml.tuning.RandomRanges._ + +case class Limits[T: Numeric](x: T, y: T) + +private[ml] abstract class RandomT[T: Numeric] { + def randomT(): T + def randomTLog(n: Int): T +} + +abstract class Generator[T: Numeric] { + def apply(lim: Limits[T]): RandomT[T] +} + +object RandomRanges { + + private val rnd = new scala.util.Random + + private[tuning] def randomBigInt0To(x: BigInt): BigInt = { + var randVal = BigInt(x.bitLength, rnd) + while (randVal > x) { + randVal = BigInt(x.bitLength, rnd) + } + randVal + } + + private[ml] def bigIntBetween(lower: BigInt, upper: BigInt): BigInt = { + val diff: BigInt = upper - lower + randomBigInt0To(diff) + lower + } + + private def randomBigDecimalBetween(lower: BigDecimal, upper: BigDecimal): BigDecimal = { + val zeroCenteredRnd: BigDecimal = BigDecimal(rnd.nextDouble() - 0.5) + val range: BigDecimal = upper - lower + val halfWay: BigDecimal = lower + range / 2 + (zeroCenteredRnd * range) + halfWay + } + + implicit object DoubleGenerator extends Generator[Double] { Review comment: OK that's fine. I guess I mean do we really need implicits to manage a little logic reused twice internally? Then again I'm kind of primitive when it comes to using implicits. If its user-visible is it any problem for Java callers as an additional arg they need to pass - no right? that's why I was wondering how public it needs to be. No big deal. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
