diff --git a/docs/docs/reference/builtin-functions/feel-built-in-functions-numeric.md b/docs/docs/reference/builtin-functions/feel-built-in-functions-numeric.md index e962ef2ef..5406b06f0 100644 --- a/docs/docs/reference/builtin-functions/feel-built-in-functions-numeric.md +++ b/docs/docs/reference/builtin-functions/feel-built-in-functions-numeric.md @@ -254,3 +254,16 @@ even(5) even(2) // true ``` + +## random number() + +Returns number between 0 and 1 + +- parameters: + - NaN +- result: number + +```js +random number() +// 0.9701618132579795 +``` \ No newline at end of file diff --git a/src/main/scala/org/camunda/feel/impl/builtin/NumericBuiltinFunctions.scala b/src/main/scala/org/camunda/feel/impl/builtin/NumericBuiltinFunctions.scala index bbefb38c3..2ebca4369 100644 --- a/src/main/scala/org/camunda/feel/impl/builtin/NumericBuiltinFunctions.scala +++ b/src/main/scala/org/camunda/feel/impl/builtin/NumericBuiltinFunctions.scala @@ -14,6 +14,7 @@ import org.camunda.feel.syntaxtree.{ } import scala.math.BigDecimal.RoundingMode +import scala.util.Random object NumericBuiltinFunctions { @@ -32,7 +33,8 @@ object NumericBuiltinFunctions { "round up" -> List(roundUpFunction), "round down" -> List(roundDownFunction), "round half up" -> List(roundHalfUpFunction), - "round half down" -> List(roundHalfDownFunction) + "round half down" -> List(roundHalfDownFunction), + "random number" -> List(randomNumberFunction) ) private def decimalFunction = builtinFunction( @@ -164,4 +166,10 @@ object NumericBuiltinFunctions { case List(ValNumber(n), ValNumber(scale)) => round(n, scale, RoundingMode.HALF_DOWN) }) + + private def randomNumberFunction = + builtinFunction(params = List(), invoke = { + case List() => + ValNumber(Random.nextDouble()) + }) } diff --git a/src/test/scala/org/camunda/feel/impl/builtin/BuiltinNumberFunctionTest.scala b/src/test/scala/org/camunda/feel/impl/builtin/BuiltinNumberFunctionTest.scala index 07696835d..58e30883a 100644 --- a/src/test/scala/org/camunda/feel/impl/builtin/BuiltinNumberFunctionTest.scala +++ b/src/test/scala/org/camunda/feel/impl/builtin/BuiltinNumberFunctionTest.scala @@ -17,6 +17,7 @@ package org.camunda.feel.impl.builtin import org.camunda.feel.impl.FeelIntegrationTest +import org.camunda.feel.syntaxtree import org.camunda.feel.syntaxtree._ import org.scalatest.matchers.should.Matchers import org.scalatest.flatspec.AnyFlatSpec @@ -259,4 +260,17 @@ class BuiltinNumberFunctionsTest eval(" round half down(-1.126, 2) ") should be(ValNumber(-1.13)) } + + "A random number() function" should "return a number" in { + + eval(" random number() ") shouldBe a [ValNumber] + } + + it should "return a number between 0.0 and 1.0 " in { + + eval(" random number() ") match { + case ValNumber(x) => x should (be >= BigDecimal(0) and be <= BigDecimal(1)) + case other => fail() + } + } }