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

Random Choice

Posted by Jimmy Gunawan on Dec 13, 2009

I am thinking to play around with text and string input, I wonder at the current version of NodeBox2, it is possible to do? I tried to input using Copy Stamp, but I believe something is missing.

Thinking to try to create this "Joshua Davis" style:
http://nodebox.net/code/index.php/Strings

I try to code the text node like this below:
words = ["NodeBox", "Ekky-ekky!", "z'Boing!"]
c = choice(words)
t = Text(words, self.x, self.y, self.width, self.height)
...

Did not seem to understand choice(). Not sure why this does not work.

Another thing, I am wondering if there is a way to randomize object/shapes filter out the output? Say, for example, I have SVG shapes A, B, C, and I would like to mix and match, randomize and filter them, so I get either A, B, or C. I think this needs a special node.

Looking forward for next update ;) Cheers guys.


 
Posted by Lucas on Dec 14, 2009

Hi Jimmy,

use:

from random import choice
That shall do it.



Posted by Jimmy Gunawan on Dec 15, 2009

Hi Lucas,

I added that line of code and it works great!

At the moment, the random gives random choice everytime I press refresh. I think there is another thing I need to know, a code/function like "seed", to keep choice in random a more predictable result. At first, I thought "Wiggle" node is similar to this (sort of), but it works on Vector and not String.

Thanks again. I will experiment with choice() for now and read further the documentation.



Posted by lievn on Dec 15, 2009

you can also import seed
f.i. if you take a textpath node and add a seed parameter to it (metadata) you can change the code that it calculate the modulo between the seed and the length of the textstring:

from random import seed,choice
 
def cook(self):
    t = len(self.text) # length of text
    n = self.seed%t # modulo 
    tp = self.text[n] # use that number to select a letter
    t = Text(tp, self.x, self.y, self.width, self.height)
    # rest of the textpath node code as it is



Posted by Jimmy Gunawan on Dec 16, 2009

Hi lievn,

Thanks for additional info on seed. I will try to add this code.

I seriously need to take time to learn programming a bit.

And from this part, I wonder how to use it with copy stamp and CNUM?

Randomize string of text seems so be still complicated. Or maybe it is possible, it is just my understanding of NodeBox is not there yet.



Posted by Josh Caswell on Dec 16, 2009

Jimmy, you can look at the documentation for python's random module.

If you don't know much programming, I suppose it may not make a lot of sense.

Quick explanations: random() is a function that works a certain mathematical formula to give you a different number each time. Every time you call it, it starts from the last value it gave you. The first time you call it, it starts from the value you pass to seed(). (Python will call seed for you if you don't, using the current computer clock time.) If you put this in a script:

from random import seed, random
 
seed(10) # or any other number you want
 
print random()
the same number will be printed every time. This is how you can use seed to "control" the result of random.

Also, there is a shuffle() function in the random module. You can pass a list to this function, and get a random reordering. Then you can take the items from your list one by one (with no repeating), but get a random order:
from random import shuffle
 
s = "You bet!"
s = list(s)    # convert to list; can't shuffle a string
 
words = ["NodeBox", "Ekky-ekky!", "z'Boing!"] 
 
shuffle(s)
shuffle(words)
 
for i in range(3):
    print s.pop(0)
    print words.pop(0)
Hope that is helpful and not too confusing!



Posted by Jimmy Gunawan on Dec 18, 2009

Cheers Josh, that shuffle thing is very useful!

I will try get my head to understand this code. It will take a bit of time.

And yes, that's so true, I need to learn Python as well.



Posted by Jimmy Gunawan on Dec 18, 2009

I tried combining lievn and as well Josh's coding.

SEED is actually very useful, I could randomize the stamping using that attribute. Especially for Text.

This is cool, I think I got it this time. Thanks all, I'll continue the experiment.