Randomness in NodeBox OpenGL

zahar othman's Avatar

zahar othman

26 Jun, 2014 08:59 AM

Convert few codes from NodeBox 1 examples in Mac to NodeBox OpenGL for my students as most of them use Windows version.

What I notice is that whenever a random command is issued inside the draw method.

say for example: strokewidth(random(1,5))
or any command using random

In Window when the canvas.run(draw) is issued , what being displayed is a blinking screen where the random values keep on changing. This will make random function ineffective inside the draw method. Is it a bug of the run loop?

  1. 1 Posted by Stuart Axon on 16 Nov, 2015 10:28 AM

    Stuart Axon's Avatar

    Hopefully this late reply will be useful to someone else...

    If you want to keep the same random value every time you draw a frame, calculate it outside the draw loop, then reuse the same value.

  2. Support Staff 2 Posted by Frederik De Ble... on 15 Dec, 2015 01:17 PM

    Frederik De Bleser's Avatar

    Randomness in software is tricky. Since the code is executed every frame, and random returns a different value every time it is called, the result will be different for every frame.

    There are two solutions to this:

    1. Pre-calculate the values: that is, they should be random at initialization time (every time the program runs), not for every frame.
    2. Use a random seed. Use from random import seed at the top, then in your draw loop, set the seed to a fixed value, like seed(42). Random numbers are not really random, but depend on this seed to generate a series of random values. The same seed always calculates the same series. So by "locking" the seed to a fixed value in the draw call, we can make sure that the values will not change per-frame.

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

Recent Discussions

22 Feb, 2020 11:34 AM
22 Feb, 2020 05:15 AM
17 Feb, 2020 03:30 PM
06 Feb, 2020 12:30 AM
03 Feb, 2020 01:01 AM

 

03 Feb, 2020 12:16 AM
31 Jan, 2020 08:25 PM
31 Jan, 2020 08:21 PM
27 Jan, 2020 06:44 AM
26 Jan, 2020 10:13 AM
31 Dec, 2019 12:25 AM