Kurbamit
Kurbamit

Reputation: 105

How to randomly pick number of 2 numbers. p5js

I think it is a very simple question, but I can't find an answer. How to randomly pick one number out of 2 numbers. In my situation, I need to pick numbers {-2, 2}. Just one out of these two numbers.

Upvotes: 0

Views: 1642

Answers (2)

Owen
Owen

Reputation: 790

You can pass an array of items into random() if you need to randomly choose from a predetermined set.

In your case, the following code will always pick (-2 or 2), nothing else or anything in-between.

random([-2, 2]);

You can read more about the random function here https://p5js.org/reference/#/p5/random

Upvotes: 1

OO7
OO7

Reputation: 690

When you needs to get same random value for all clients at the same time (so all clients will get the same random value without having to talk over the network), you can use randomSeed() as pseudo-random numbers generator (PRNG).

Upvotes: 0

Related Questions