### How do I use random numbers in CodeWorld?

251
views
0
9 months ago by
How do I use random numbers in CodeWorld?
Community: CodeWorld

0
9 months ago by
There are different ways to use random numbers in CodeWorld, depending on exactly what you want.

The first thing you need to know is that computers usually don't actually use random numbers.  They use something called "pseudo-random" numbers.  Pseudo-random numbers aren't actually picked at random; they are calculated with math operations, but just calculated in a way that's so complicated that they don't follow a recognizable pattern.  To do these math operations, you need a number to start with, and that number is called a seed.  If you start with the same seed, you'll always get the same random numbers.  But at least they won't follow a pattern!

You can get a list of random numbers from a seed by using the randomNumbers function.  The list you get back is infinitely long, but if you only want a few random numbers, you can use the first function to just pick the first few numbers from it.  The numbers will be picked randomly between zero and one, but you can pick from a different range by multiplying and adding to the number.  For example, if you want random numbers between 1 and 6, you could pick a number from the list, multiply it by 5, and then add 1.

Putting all that together, here's an example that puts three dots at random places on the screen.  (The x and y coordinates are picked between -10 and 10.)

program = drawingOf(dots)

dots = translated(dot, 20 * a - 10, 20 * b - 10) &
translated(dot, 20 * c - 10, 20 * d - 10) &
translated(dot, 20 * e - 10, 20 * f - 10)
where [a, b, c, d, e, f] = first(randomNumbers(42), 6)

dot = solidCircle(1/2)​

Another technique that is sometimes easier is to make a list of the possibilities, and then shuffle it into a random order using the shuffled function.  Here is a program that uses a list comprehension to draw dots with random combinations from a list of x coordinates, and a list of y coordinates.  Again, the 1 and 2 arguments to the shuffled function are seeds.

program = drawingOf(dots)

dots = pictures([ translated(dot, x, y)
| x <- shuffled([-8, -4, 0, 4, 8], 1)
| y <- shuffled([-8, -4, 0, 4, 8], 2) ])

dot = solidCircle(1/2)​

One thing you might notice about this is that even though the positions look random, they stay the same every time the program runs.  That's because if you use the same seeds, then you always get the same numbers!  In CodeWorld, the simplest programs - drawings and animations - don't change as you run them several times, so they will always look the same when you run them twice.  To pick different random numbers, you can just change the seeds, and then rerun the program.

Once you get to simulations, though, you can create programs that do different things each time.  Simulations get a list of random numbers as the argument to their initial state, and the list is different each time.  Here's an example:

program = simulationOf(initial, step, picture)

initial(rs) = first(rs, 6)
step(rs, dt) = rs

picture([a, b, c, d, e, f]) =
translated(dot, 20 * a - 10, 20 * b - 10) &
translated(dot, 20 * c - 10, 20 * d - 10) &
translated(dot, 20 * e - 10, 20 * f - 10)

dot = solidCircle(1/2)​

Try running this a few times, and you'll notice that the dots appear in different places every time.