Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(NumericBuiltinFunction): Add random number function #528

Merged
merged 8 commits into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import org.camunda.feel.syntaxtree.{
}

import scala.math.BigDecimal.RoundingMode
import scala.util.Random

object NumericBuiltinFunctions {

Expand All @@ -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(
Expand Down Expand Up @@ -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())
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
}
}
}