1. NodeBox 1
    1. Homepage
    2. NodeBox 3Node-based app for generative design and data visualization
    3. NodeBox OpenGLHardware-accelerated cross-platform graphics library
    4. NodeBox 1Generate 2D visuals using Python code (Mac OS X only)
  2. Gallery
  3. Documentation
  4. Forum
  5. Blog

how to use the previous number generated by

Posted by Augusto Carmo on Feb 06, 2008

how to use the previous number generated by
random()?


 
Posted by anonymous on Feb 06, 2008

Set it into a variable, as follows:

x = random() # Set x to a random number
print x
oval(10, 10, x, x) # x is always the same value
Hope this helps!



Posted by Augusto on Feb 07, 2008

Yes, but I am making a loop with a random within it, that will generate a random
Position of an object, eg

X1 = random (50)
Y1 = random (50)

X2 = next value generated by random
Y2 = next value generated by random
oval (x, y, 10,10)
line (x1, y1, x2, y2)

Actually I do not need the previous value, but the next value generated at random ()
Understand + -?

sorry about my english...



Posted by fdb on Feb 07, 2008

There are a few ways to do this. One of the easiest is to remember the previous numbers in global variables, and use these, e.g.

speed(10)
 
def setup():
    global prevx, prevy
    prevx, prevy = random(50), random(50)
    
def draw():
    global prevx, prevy
    x, y = random(50), random(50)
    oval(x-5, y-5, 10, 10)
    line(prevx, prevy, x, y, stroke=0)
    prevx, prevy = x, y
Now, if what you want to implement are trails, then you're better of using a list of previous coordinates. Here's an example:
size(200, 200)
speed(10)
 
def setup():
    global trails
    trails = []
    
def draw():
    global trails
 
    x, y = random(200), random(200)
    trails.append((x, y))
    
    x0, y0 = trails[0]
    for x1, y1 in trails[1:]:
        line(x0, y0, x1, y1, stroke=0)
        x0, y0 = x1, y1
 
    oval(x-5, y-5, 10, 10)
    trails = trails[-10:]
There are a few things to note here:
- I create the random point first, and append it to trails directly, so I can access trails[0].
- I start drawing the trails from the second element(trails[1]), because I already stored the first position in x0, y0.
- The last line leaves only the last 10 trails.

If there are any more questions with this, I'll be happy to answer them.