Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
02-17-2021 10:48 AM
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.
02-17-2021 11:29 AM
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);
02-18-2021 01:02 PM
Hello @dana.canzano , but how can I generate random numbers between two specific numbers like 2 and 8?
02-18-2021 01:11 PM
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.
02-18-2021 01:20 PM
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;
02-18-2021 01:25 PM
thank you again @andrew.bowman , but are you sure the end result will be always lowered than $upper
?
02-18-2021 01:28 PM
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.
02-18-2021 02:53 PM
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.
All the sessions of the conference are now available online