Command for Random Function?

Is there a command where I can assign a random value to a variable? I’ve tried using rand(), but it kept giving me the same result of a very large number. I just need to have an int variable get a value between the range of 1-3.

The rand() command on its own will return a value between 0 and RAND_MAX. To get a random number from 1-3, you want to use rand() % 3 + 1. The number after the modulus operator sets the range from 0 to that number - 1, and then you can add a number afterwards to start at something other than 0.

4 Likes

It appears to be only selecting the number 3 every time. Am I doing something wrong?

titleBGRand = rand() % 3 + 1;

did you include <stdlib.h>?

I just added it and it still only sets it to the value of 3

Can you send more of your code?

class Menu
{
public:
void main()
{
int titleBGRand;
std::string titleBG;

  Brain.Screen.setPenColor("#A1A1A1");
  Brain.Screen.setFont(prop60);

  for (int count = 0; count < 300; count++)
  {
    Brain.Screen.printAt(count-200,50,"Main");
    Brain.Screen.printAt(540-count,50,"Menu");
    task::sleep(5);
  }

  while (!Controller1.ButtonA.pressing())
  {
    titleBGRand = rand() % 3 + 1;
    Brain.Screen.print(titleBGRand);
    if (titleBGRand == 1)
      titleBG = "marioBG.gif";
    else if (titleBGRand == 2)
      titleBG = "minecraftBG.gif";
    else
      titleBG = "skrekBG.png";
    vex::Gif gif(titleBG.c_str(), 0, 0 );
    Brain.Screen.render();

    if (menuSel == 1)
      Brain.Screen.setPenColor(blue);
    else
      Brain.Screen.setPenColor(white);
    Brain.Screen.drawRectangle(50,100,100,100);
    if (menuSel == 2)
      Brain.Screen.setPenColor(blue);
    else
      Brain.Screen.setPenColor(white);
    Brain.Screen.drawRectangle(190,100,100,100);
    if (menuSel == 3)
      Brain.Screen.setPenColor(blue);
    else
      Brain.Screen.setPenColor(white);
    Brain.Screen.drawRectangle(330,100,100,100);

    Brain.Screen.setPenColor(white);
    Brain.Screen.setFont(mono30);
    Brain.Screen.printAt(70,150,"PLAY");
    Brain.Screen.printAt(210,150,"EDIT");
    Brain.Screen.printAt(340,150,"EXTRA");

    while (Controller1.ButtonA.pressing() || Controller1.ButtonRight.pressing() || Controller1.ButtonLeft.pressing())
    {
      task::sleep(10);
    }
    while (!(Controller1.ButtonA.pressing() || Controller1.ButtonRight.pressing() || Controller1.ButtonLeft.pressing()))
    {
      task::sleep(10);
    }

    if (Controller1.ButtonRight.pressing() && !Controller1.ButtonLeft.pressing())
      menuSel++;
    else if (Controller1.ButtonLeft.pressing())
      menuSel--;
    
    if (menuSel > 3)
      menuSel = 1;
    else if (menuSel < 1)
      menuSel = 3;
  }
  Brain.Screen.clearScreen();
}

}

Try using std::rand().

It says it’s not a part of the library std

Edit: It appears to recognize that rand() on its own is a command, but it just doesn’t seem to work properly

rand() works, but everytime you run your program it will follow the same sequence unless you seed it. Looks like there are a few other issues with that code, perhaps just get rand working without all the gif display code

4 Likes

Ok it looks like the problem is that the RNG algorithm hasn’t been initialized. Add #include <time.h> and add srand(time(NULL)); to your code to initialize the RNG before using rand().
http://www.cplusplus.com/reference/cstdlib/rand/
http://www.cplusplus.com/reference/cstdlib/srand/

2 Likes

It appears to have decided that now it will always choose the number 1, so progress, but also not. I really appreciate your help btw. I’ll try researching it on my own, but any input would be greatly appreciated

Vex has a built in timer which might be conflicting with it. Replacing the line with srand(timer::system()); and make sure it’s the line before the rand() declaration.

5 Likes

After a bit of messing around with this command and where it was placed in the code, I finally got it working. Thank you so much!

1 Like

glad you figured it out, for anyone else, it’s not that hard, here is a simple example.

int main() {
    int number = 0;
   
    srand( Brain.Timer.systemHighResolution() );

    while(1) {
        number = rand() % 10;

        Brain.Screen.printAt( 10, 50, "number is %d", number);
        // Allow other tasks to run
        this_thread::sleep_for(200);
    }
}
8 Likes

I did have a bit of trouble with just initializing it at the beginning of the code. It would pretty consistently choose just one number, but once I initialized it after a user input, it seemed to work better.

Mayber 3 is random :wink:…:
image

8 Likes

image

If we are posting fun random number comics.

And as for @CowPig127 its the nature of all software random number generators that they are truly fake(psuedo-random). For each seed value there is a fixed order of random numbers you get. If I seeded the random number generator with 473 it would always give me the same numbers every time. So the common practice is seed the generator with the current time. This means the seed is always different and so is the sequence returned by the generator. However a lot of hobby electronics only have a clock from when your program started so you end up with a time of say 1ms every boot. ( I imagine this is why James used systemHighResolution, and why you find that if you initialize at the start of your program you get the same results)

A high enough resolution in theory means its 1.0001 ms vs 1.0002ms keeping with my example before. Or you can do something like initialize the clock after the user presses a button which will always be a different amount of time.

5 Likes