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

Proposal: Add functions to generate random values #390

Open
jhorbulyk opened this issue Nov 20, 2019 · 5 comments
Open

Proposal: Add functions to generate random values #390

jhorbulyk opened this issue Nov 20, 2019 · 5 comments

Comments

@jhorbulyk
Copy link
Contributor

jhorbulyk commented Nov 20, 2019

It would be nice if JSONata had the ability to generate random values. Consider the following two cases:

  • Generate a random number between 0 and 1 (similar to Math.random() in JavaScript.)
  • Generate a random GUID/UUID. (Harder to do without pulling in something like uuid as a dependency.)
@andrew-coleman
Copy link
Member

We can do the first one already - https://docs.jsonata.org/numeric-functions#random

@markmelville
Copy link
Contributor

markmelville commented Nov 20, 2019

I looked at some of the javascript implementations for uuid and worked out this expression that generates a v4 UUID:

(
  $binPad:=function($n, $len){$pad($formatBase($n,2),-$len,'0')};
  $bitwise:=function($lb, $rb, $fn){$split($lb,'')~>$map(function($c, $i){$fn($c='1',$substring($rb,$i,1)='1')?$power(2,(7-$i)):0})~>$sum()};
  $and:=function($l,$r){$bitwise($binPad($l,8),$binPad($r,8),function($1,$2){$1 and $2})};
  $or:=function($l,$r){$bitwise($binPad($l,8),$binPad($r,8),function($1,$2){$1 or $2})};  
  [0..15]~>$map(function(){$floor($random()*256)})
    ~>$map(function($v,$i){$i=6?$or($and($v,15),64):$i=8?$or($and($v,63),128):$v})
    ~>$map(function($n,$i){$pad($formatBase($n,16),-2,'0') & ($i in [3,5,7,9]?'-':'')})
    ~>$join()
)

What would have made this simpler is if there were bitwise operators built-in to Jsonata, but that should be a separate request, I suppose.

@zubairov
Copy link

Though issue with random solved I strongly believe the $uuid() will be a beneficial addition to JSONata

@adamgins
Copy link

adamgins commented May 30, 2022

HI does any one have an example where would adjust above not to be HEX, I am actually just looking for a random set of characters?

update: I just made something like

(
  $charSet := ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" ,"!","-"];  
  $randomChar := function(){
      (
          $randomCharChosen := $floor($random()* $count($charSet));
          $charSet[$randomCharChosen];
           

      )
      };
 
$reduce([0..10],function($v,$i){( $v & $randomChar();)})

)

@dubnemo
Copy link

dubnemo commented Feb 12, 2024

Though issue with random solved I strongly believe the $uuid() will be a beneficial addition to JSONata

It seems there isn't a separate issue related to UUID() and it seems like there is consensus. I need it inside a map, to generate a new one as I iterate through an array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants