Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Random values in Draw?

edited December 2011 in Questions Posts: 4

Hi, I have a grid of circles iterated on the x and y axis. I want to assign individual random sizes to them but because of the draw function the random numbers change every frame and animate the size rather than assigning a static value to each object. Suggestions? Thank you!

Tagged:

Comments

  • edited December 2011 Posts: 273

    Hi Superflea... one way to approach this would be to create a table in the setup() function with a random size for each ellipse and then iterate through this table in your draw routine (using ipairs) to assign the random size to each ellipse...

    HTH!

  • SimeonSimeon Admin Mod
    edited December 2011 Posts: 5,363

    Another solution would be to set the random seed at the start of the draw() call. This ensures that exactly the same sequence of numbers is generated each time:

    function draw()
        background( 0 )
    
        math.randomseed( 1234 )
    
        ellipse( 100, 100, math.random( 10, 100 ) )
    
        ellipse( 200, 200, math.random( 10, 100 ) )
    
        ellipse( 300, 300, math.random( 10, 100 ) )
    end
    

    Edit: I think @Blanchot's solution is better. This is just an alternative.

  • Thank you for the replies, I'll give it a try and let you know!

  • Worked like a charm, thanks!

    For the record, I used Simeon's method, but added a random in the setup() to provide the seed for the randomseed, so that each time I run the program the outcome is different.

    Must say that Codea is proving hugely instructive and fun!

Sign In or Register to comment.