Rand or Random for random numbers?

In arduino, it’s random(min, max), but I was typing in the Spark Build and it highlighted just rand. Is that the built-in function? I saw some examples using function like:

int random(int max) {
  return random(0, max);
}

int random(int minVal, int maxVal)
{
  // int rand(void); included by default from newlib
  return rand() % (maxVal-minVal+1) + minVal;
}

I wonder why do we have wrap the rand() function that way?

You won’t have to do this for much longer :slight_smile:

1 Like

Nice! Looking fwd to!

@metaculus, having @mdma on the Spark Team rocks! Until that change makes it to production, you can look at @kennethlimcp’s repo for another topic (spark_wiring_random.cpp & .h) for files you can use. :smile:

2 Likes

Thanks, but I can’t take the credit for this one, it was @zachary who did the lion’s share of the work here. I just added some unit tests.

Equally exciting is that the firmware can how get a random seed from the cloud (from a cryptographically secure source) so sequences start from a very randomized seed.

1 Like

I’m guessing the work from this is in production now? I’m hitting the following error if I try to use randomSeed(analogRead(A0));

blink.cpp:1:11: error: expected constructor, destructor, or type conversion before '(' token

The surrounding code of your suspected trouble line would be good.

as @ScruffR says, the error report says that you are calling this on line 1 of the blink.cpp which is not likely to work. Did you mean to call it in setup() or loop() or some other function instead?

Ah, didn’t know this had to be inside of a function, originally had the following just to isolate the issue. Looks like throwing it inside setup or loop works fine.

randomSeed(analogRead(A0));

void setup() {}
void loop() {}