Random Number Generation in PROS?

How do I generate random numbers in PROS? When I try to use srand(time(NULL)) to seed the RNG, I get an error that __gettimeofday is undefined.

you get that error because the cortex does not have a clock so it cant link with the FreeRTOS time library.

In order to get psudorandom numbers that dont simply repeat every time the cortex restarts you need entropy sources other than the time, here are the best sources i personally use:

*]line sensors and potentiometers
*]battery voltages
*]ultrasonic sensors
*]unused analog ports, these usually vary by ±8 every time you read them

optionally you can save the seed in flash memory and load it back on startup for extra entropy

the RNG i use is a simple xorshift like similar to this one but i use multiple layers, i xor the entropy values with the current seed, apply the xorshift, etc

uint64_t xorshift(uint64_t long x) {
  x^=rrot(x,35); // right rotate operation btw
  return x;

you probably arent using it for cryptography, though so just something like this will most likely work for you:

if (analogRead(unused_analog_port)^1) {

} else {


or if you want a random integer (you can also feed it into srand)

void rng(unsigned int* x,unsigned int n) {

unsigned int newseed() {
	unsigned int seed=0xbf66e081; // magic
	for (int i=1;i<=8;i++) rng(&seed,analogRead(i));
	return seed;