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:

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

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^=x<<21;
  x^=rrot(x,35); // right rotate operation btw
  x^=x<<4;
  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) {
	*x^=n;
	*x^=*x<<21;
	*x^=(*x<<35)|(*x>>((sizeof(*x)*8)-35));
	*x^=*x<<4;
}

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