**Take Control**– Choose how to license your stuff and who can edit it, track every change Join Siafoo Now or Learn More

# biased random generator 2

In Brief | The advantage of randomness in generative art is that it produces variation. The drawback is that it introduces chaos by lacking any form of symmetry and coherence. This function offers an easy go-between by allowing you to say: "I want a composition with random ovals, most of which are small, and most of which are located at the bottom left", instead of simply a haphazard collection of ovals all over the place.... more |

Language | Nodebox Python |

# 's

` 1from random import random as rnd`

2from math import floor

3

4_map = [90.0, 9.00, 4.00, 2.33, 1.50, 1.00, 0.66, 0.43, 0.25, 0.11, 0.01]

5def random(v1=1.0, v2=None, bias=0.5):

6 """ Returns a number between v1 and v2, including v1 but not v2.

7 The bias represents the preference towards lower or higher numbers,

8 as a number between 0.0 and 1.0. For example:

9 random(0, 10, bias=0.9) will return 9 much more often than 1.

10 """

11 if v2 == None:

12 v1, v2 = 0, v1

13 # We use bias*10 as index in the _map curve.

14 # If bias is 0.3, rnd()**2.33 will average 0.3.

15 bias = max(0, min(bias, 1)) * 10

16 i = int(floor(bias))

17 n = _map[i]

18 if bias < 10:

19 n += (_map[i+1]-n) * (bias-i)

20 # For rnd() yielding numbers between 0.0 and 1.0:

21 # we do rnd()**n

22 # n > 1: random numbers will be closer to 0.

23 # 0 < n < 1: numbers will be closer to 1.

24 x = rnd()**n * (v2-v1) + v1

25 if isinstance(v1, int) and isinstance(v2, int):

26 x = int(x)

27 return x

28

29# NodeBox example:

30size(500, 500)

31for i in range(100):

32 fill(random(bias=0.1)) # more black

33 r = random(10, 100, bias=0.2) # more small ovals

34 oval(

35 random(WIDTH-r, bias=0.25), # more to the left

36 random(HEIGHT-r, bias=0.75), r, r # more to the bottom

37 )

38

39print random(0,10, bias=0.9)

The advantage of randomness in generative art is that it produces variation. The drawback is that it introduces chaos by lacking any form of symmetry and coherence. This function offers an easy go-between by allowing you to say: "I want a composition with random ovals, most of which are small, and most of which are located at the bottom left", instead of simply a haphazard collection of ovals all over the place.

Add a Comment