Random number issue!

//player 1 LED
int P1blue = 2;
int P1red = 3;

//player 2 LED
int P2blue = 4;
int P2red = 5;

int second; // decides who goes first

void setup()
{
  pinMode(P1red, OUTPUT);
  pinMode(P1blue, OUTPUT);
  pinMode(P2red, OUTPUT);
  pinMode(P2blue, OUTPUT);

second = random(2) +1;  // Generate a random number between 1 and 2.


//play game start and aniticipation
  digitalWrite(P1blue, HIGH);
  digitalWrite(P2blue, HIGH);
  delay(300);
  digitalWrite(P1blue, LOW);
  digitalWrite(P2blue, LOW);
  delay(300);
  digitalWrite(P1blue, HIGH);
  digitalWrite(P2blue, HIGH);
  delay(300);
  digitalWrite(P1blue, LOW);
  digitalWrite(P2blue, LOW);
  delay(300);
  digitalWrite(P1blue, HIGH);
  digitalWrite(P2blue, HIGH);
  delay(300);
  digitalWrite(P1blue, LOW);
  digitalWrite(P2blue, LOW);

  delay(1000); // The dreaded wait of who goes first.. lol
  
  //select who goes first
    if (second == 1)
    {
        digitalWrite(P1blue, HIGH);
    }
    else
    {
        digitalWrite(P2blue, HIGH);
    }
}

void loop()
{
  
}

it's always choosing player 2... :angry:

i want it to have a 50/50 of who it picks..

You haven't seeded the generator with a random starting point so it always uses the same one. See: https://www.arduino.cc/en/Reference/RandomSeed

Pete

second = random(2) +1; // Generate a random number between 1 and 2.

huh?

says pick 1 OR 2..

so not getting this "serial" thing.. it's a basic statement.. just pick 1 OR 2.. 50/50 chance..

so unless you got a straight forward bit of code that shows me.. yer just confusing me with "serial" and "noise" stuff honestly

READ the link you were provided. random() will ALWAYS return the same sequence of numbers, if you don't seed the generator using RandomSeed(). It's usually much better to read up on how things really work, rather than waste time chasing your tail because they don't work like you want them do.

Regards, Ray L.

https://www.arduino.cc/en/Reference/RandomSeed

//player 1 LED
int P1blue = 2;
int P1red = 3;

//player 2 LED
int P2blue = 4;
int P2red = 5;

int second; // decides who goes first

void setup()
{
  randomSeed(analogRead(A0));
  pinMode(P1red, OUTPUT);
  pinMode(P1blue, OUTPUT);
  pinMode(P2red, OUTPUT);
  pinMode(P2blue, OUTPUT);

second = random(2) +1;  // Generate a random number between 1 and 2.


//play game start and aniticipation
  digitalWrite(P1blue, HIGH);
  digitalWrite(P2blue, HIGH);
  delay(300);
  digitalWrite(P1blue, LOW);
  digitalWrite(P2blue, LOW);
  delay(300);
  digitalWrite(P1blue, HIGH);
  digitalWrite(P2blue, HIGH);
  delay(300);
  digitalWrite(P1blue, LOW);
  digitalWrite(P2blue, LOW);
  delay(300);
  digitalWrite(P1blue, HIGH);
  digitalWrite(P2blue, HIGH);
  delay(300);
  digitalWrite(P1blue, LOW);
  digitalWrite(P2blue, LOW);

  delay(1000); // The dreaded wait of who goes first.. lol

  //select who goes first
    if (second == 1)
    {
        digitalWrite(P1blue, HIGH);
    }
    else
    {
        digitalWrite(P2blue, HIGH);
    }
}

void loop()
{

}

so unless you got a straight forward bit of code that shows me.. yer just confusing me with "serial" and "noise" stuff honestly

Have you used the forum's search box?

Another way to do this is to wait until a button is pressed (not the Arduino reset button: for this you have to attach your own button to the Arduino), and then use millis() as your seed.

meh, dunno why i come here for help..

i showed code.. i asked for code.. i get crap over a simple thing...

my god, sorry not everyone is an expert at arduino!

You showed code, you got (pretty damned accurate) answers. What's your problem?

yer just confusing me with "serial" and "noise" stuff

What serial thing? what noise? I said SEED. Read the link I gave you. Carefully. It explains what you need and even has a piece of code that shows how to generate a (more or less) random seed so that you won't get 2 every time.

Pete

meh, dunno why i come here for help..

i showed code.. i asked for code.. i get crap over a simple thing...

my god, sorry not everyone is an expert at arduino!

Copy and pasting is OK if that's your thing.

May be you should have ask: Give me code that works, give me the facts only the facts as for understanding I don't need to.

Then we would know where you were coming from. Most people who come here including me want to understand and be taught. .

it was a simple question..

i showed code..

i asked for code..

i don't want just code.. i want to know what each line is "commented" so i see WHY - and that's how i'll learn..

otherwise, referring me to other pages with just more text upon text upon text.. gets me nowhere - i can read all i want til i'm blue in the face.. i have a certain project to accomplish - i don't need or want "general" info.. i want it explained how it is and why it is.

this may not be a classroom, but let's try to teach as if it were...

this //because this is this

this // because it does this, a shorter way

yada yada.. c'mon.. i know some of you may have forgotten what it's like to be new, some want to be called experts and all that.. but let's teach instead of thinking you are better or we all should know more than what we do ok?

so i gave you my code.. where did i go wrong and why? how can i improve my code with your coding snips to accomplish exactly what i want to do? please explain why.. so others may learn as well.. keep in mind yer not just teaching me.. but others who may come into arduino that may have the same exact thing to accomplish.

Did you read reply #4?

Did you try it?

What was wrong with it?

insignia: it was a simple question..

insignia: it's always choosing player 2... :angry:

i want it to have a 50/50 of who it picks..

I can not see a question here.

i have a certain project to accomplish

4 has your answer.

Now we understand. :art:

insignia: it was a simple question..

i showed code..

i asked for code..

i don't want just code.. i want to know what each line is "commented" so i see WHY - and that's how i'll learn..

otherwise, referring me to other pages with just more text upon text upon text.. gets me nowhere - i can read all i want til i'm blue in the face.. i have a certain project to accomplish - i don't need or want "general" info.. i want it explained how it is and why it is.

this may not be a classroom, but let's try to teach as if it were...

this //because this is this

this // because it does this, a shorter way

yada yada.. c'mon.. i know some of you may have forgotten what it's like to be new, some want to be called experts and all that.. but let's teach instead of thinking you are better or we all should know more than what we do ok?

so i gave you my code.. where did i go wrong and why? how can i improve my code with your coding snips to accomplish exactly what i want to do? please explain why.. so others may learn as well.. keep in mind yer not just teaching me.. but others who may come into arduino that may have the same exact thing to accomplish.

You act like we owe you something. If you don't like the help here, go read the documentation for yourself. We were all newbies once, and we learned by reading the docs. Fancy that you've got folks here to point you to the right docs to read. If you want it spoonfed, well that's not what we want to do. And you can't make us. And we don't owe you anything. If you are unhappy with the service here, then here _____ there's quadruple your money back.

i was gonna be nice… but Delta_G… [edited for content]

it’s people like you when you say shit just like you did - makes this place shit!

[Watch the language insignia, or you’re at risk of being banned]

And if you think it hurts anyone's feelings that you're unhappy then think again. We're all having a good laugh at your expense right now. Look at this guy, we tell him what he needs to know and then he's mad because we didn't write it in code for him. What an idiot if he can't understand the doc for randomSeed.

insignia:
it’s people like you when you say shit just like you did - makes this place shit!

No, it’s people like you bud who expect us to do all your work for you and don’t think you need to read to learn. You lazy…

If you think it is so shitty, then don’t come back. Good riddance.

You must be pulling our leg. Seriously now, have you read: https://www.arduino.cc/en/Reference/RandomSeed https://www.arduino.cc/en/Reference/Random