cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Cypher query and rand() function

maziar
Node Link

I want to generate random integer numbers between 0 and 10, using the rand() in my Cypher query, ( because there is a rating property I want to randomly feed).
Hence, please help me to generate those random numbers.
thank you in advance.

8 REPLIES 8

rand() will generate a random number from 0 (inclusive) to 1 (exclusive); i.e. [0,1) Mathematical functions - numeric - Neo4j Cypher Manual .

And so for example

return rand();

may return

0.340139196576676

rerun return rand(); and the result is now

0.9742259982450796

so if you want a random number between 0 and 10

return toInteger(rand()*11);

Hello @dana.canzano , but how can I generate random numbers between two specific numbers like 2 and 8?

Take the lower bound, subtract it from the upper and lower bounds, generate the rand() between those, then add the lower bound back to the result.

So for 2 and 8, calculate the rand() for between 0 and 6, then add 2 to the result.

@andrew.bowman sorry, how would that be like?

To generate a random between 2 and 8, you can use

RETURN toInteger(rand() * 7) + 2;

Or, for a more generic approach, if you have $lower and $upper parameters, you can do:

RETURN toInteger(rand() * ($upper - $lower + 1)) + $lower;

thank you again @andrew.bowman , but are you sure the end result will be always lowered than $upper ?

I'm not sure what you mean, but you can test this yourself here, though you'll have to set the parameters:

UNWIND range(1,200) as row
RETURN toInteger(rand() * ($upper - $lower + 1)) + $lower;

You can scan through the results yourself, or modify to filter to anything outside the bounds and see if it returns anything.

also, regarding

RETURN toInteger(rand() * ($upper - $lower + 1)) + $lower;

so we know rand() will will generate a random number from 0 (inclusive) to 1 (exclusive) and thus a number between 0 and 0.99999999999 for example and so now we have this number * ( upper-lower + 1) + lower.

if upper is 8 and lower is 2 then this is
toInteger( some value between 0 and 0.999999999 * ( 8 -2 +1 ) ) and then + 2

so thus
toInteger( some value between 0 and 0.999999999 * ( 7 ) ) and then +2

and this is thus
toInteger ( so value between 0 and 6.99999999999) + 2
or
a value between (0 and 6) + 2

and this a value between 2 and 8

but are you sure the end result will be always lowered than $upper ?

ok in this case we might generate a value equal to either $lower or $upper (i.e. we might generate a value of either 2 or 8 in this case).

if you want to generate a random value not inclusive of $upper and $lower, i.e. u want a random number > 2 and < 8 then

its not much to modify the original example accordingly of

RETURN toInteger(rand() * ($upper - $lower + 1)) + $lower;

so as to meet your needs whatever they may be.