 # Random negative numbers

Hi,
I tried to use unsigned long random(-1,1) to get random numbers from -1 to 1, but it only picks negative numbers. Can you tell me how to fix this? thanks

The high limit is exclusive.

[/quote] Hi thanks for reply, I did tried increasing the positive value to other than 1.

`random(-1, 2)`gives me -1, 0 or 1

Is that what you want ?

The implementation in the repository(s) looks OK. Could you show us exactly how you're using it (with the nearby code)?

You can't have negative numbers in unsigned values

Silente: You can't have negative numbers in unsigned values

I was wondering the same thing. How are you only getting negative numbers in a variable that is unsigned (positive representation only)?

adwsystems: I was wondering the same thing. How are you only getting negative numbers in a variable that is unsigned (positive representation only)?

Bear in mind that we have not seen any working code

Hi thanks for replies. I’m not sure what i was doing wrong but now i can get both positive and negative values. I’m using int randomDelay = random(-1, 2);
I’m making essentially a macro where i’d like to simulate real mouse movement and for that i’d like to make it prefer 0 over -1 and 1.

For reference this is the code(There is also a joystick and a coded switch like the switch tutorial):

``````#include <Mouse.h>
#include <Keyboard.h>
#include <Joystick.h>

Joystick_ Joystick;

int8_t joystickType = 0x02;
float Multiplier = 1.2;
bool includeXAxis = true;
bool includeYAxis = true;
const int Analog1 = 0;
const int Analog2 = 1;
const int Analog3 = 2;
const int Analog4 = 3;
int XValue = 0;
int YValue = 0;
int inPin = 2;         // the number of the input pin
int state = LOW;      // the current state of the output pin
int previous = HIGH;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
Serial.begin(9600);
Joystick.begin();
delay(1000);
pinMode(inPin, INPUT_PULLUP);
}

void loop() {
XValue = XValue * Multiplier;
YValue = YValue * Multiplier;
Joystick.setXAxis(XValue);
Joystick.setYAxis(YValue);

if (reading == LOW && previous == HIGH && millis() - time > debounce) {
if (state == LOW)
state = HIGH;
else
state = LOW;
time = millis();
}