How do I generate a number between 1-180 (VEX EXP)

How do I generate a number between 1-180 in vex exp

Depends on what language you are coding in.
Blocks is the easiest:
image

Python:
urandom.randint(1, 180)

C++ is more complicated:

#include <time.h> //for time

int main() {
  srand(time(NULL)); //set random seed
  
  Brain.Screen.print((rand() % 180)+1); //get random from 0 + 179 and then add 1 to result
}

I’m not sure how well this would work though because I don’t know how system time works on EXP, @jpearman might know a better way of doing this.

Side Note

When I tried this code using Mersenne twister:

Code
//include random library
#include <random>

int main() {
  //setup random number generator
  std::random_device dev;
  std::mt19937 rng(dev());
  std::uniform_int_distribution<std::mt19937::result_type> dist180(1,180);

  //call random number
  Brain.Screen.print(dist180(rng));

}

I get this error:

Error

unix build for platform vexexp

CXX src/main.cpp

In file included from src/main.cpp:49:

In file included from /home/ubuntu/cloudcompiler/Tools/sdk/vexexp/20240219_10_00_00/gcc/include/c++/7.3.1/random:38:

/home/ubuntu/cloudcompiler/Tools/sdk/vexexp/20240219_10_00_00/gcc/include/c++/7.3.1/cmath:45:15: fatal error: ‘math.h’ file not found

#include_next <math.h>

^~~~~~~~

1 error generated.

make: *** [vex/mkrules.mk:13: build/src/main.o] Error 1

make process closed with exit code : 2

Does anyone know why this is happening?

1 Like

You must be using the cloud compiler, it still seems to on on an old version of the build system. That’s caused by an issue with toolchain include path order.

1 Like

Yeah, I’m on cloud compiler. Would that not be an issue on other platforms?

The desktop apps were updated, as was VSCode. I thought the cloud compile system had been, but apparently not. It’s an issue with #include_next in the system headers. It’s probably an issue for IQ2 as well, but using C++ standard library on IQ2 is often not viable due to lack of memory. I don’t think this was ever an issue for V5 (older version of system headers).

2 Likes