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.

Random values in cypher

Hi,
is there a chance to use MIN, MAX in Cypher like in the following example, or is it possible to save the rand()- value into a variable and use the variable in the same cypher- statement.

MATCH (:Person)-[c:Meeting]->(q:Person)
WHERE (q.att1 = 0) AND
	( rand() < MIN( 0.25, MAX( 0.01, 1 / MAX( 1,  c.distance/1000.0))))
SET q.att2 = 1

You see, I'm a beginner. Thanks for your help.

Reinhold

5 REPLIES 5

Hello:)

WITH rand() AS r
MATCH (:Person)-[c:Meeting]->(q:Person)
WHERE (q.att1 = 0)
AND (r < MIN( 0.25, MAX( 0.01, 1 / MAX( 1,  c.distance/1000.0))))
SET q.att2 = 1

Thank you very much!

Reinhold

No problem, a pleasure:)

Hi,
sorry for another question. I suppose, the statement above produces only one random value for each query. Is this correct? But I need a random value for each selected record. Does following query works?

MATCH (:Person)-[c:Meeting]->(q:Person)
WHERE (q.att1 = 0)
AND (rand() < MIN( 0.25, MAX( 0.01, 1 / MAX( 1, c.distance/1000.0))))
SET q.att2 = 1

I didn't know that MIN/MAX statements works in Cypher.
And sorry for my English.

Reinhold

Yes, you right, but if you still want to keep the rand value to use it later in the query, try this:

MATCH (:Person)-[c:Meeting]->(q:Person)
WHERE q.att1 = 0
WITH q, c.distance AS dist, rand() AS r
WHERE (r < MIN( 0.25, MAX( 0.01, 1 / MAX( 1, dist/1000.0))))
SET q.att2 = 1