# Random video generated for every mouse click

**URL:** <https://discourse.processing.org/t/random-video-generated-for-every-mouse-click/14958>\
**Category:** Libraries\
**Created:** [October 27, 2019, 12:53am UTC](https://discourse.processing.org/t/random-video-generated-for-every-mouse-click/14958 "2019-10-27T00:53:10Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![szb1](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@szb1](https://discourse.processing.org/u/szb1)\
**Post date:** [October 27, 2019, 12:53am UTC](https://discourse.processing.org/t/random-video-generated-for-every-mouse-click/14958/1 "2019-10-27T00:53:10Z")

</div>

Hi,

I am trying to get a random video to play from an array but a selected number only for when I press the left or right click. For this I created two seperate variables to generate a random number. Every time I click left it seems to be working yet it is glitching. I don’t think I am going about it the right way either…I can’t quite understand what I am doing wrong.

Any help will be appreciated.

```auto
import processing.video.*;

Movie myMovie3;
Movie[] mov = new Movie [8];

final static int IDLE = 1;
final static int ANIM1 = 2;
final static int ANIM2 = 3;
int state = 1;
int count = 0;
int rIndex; //variable for random R poster to display - 6 videos 
int gIndex; //variable for random G poster to display - only two videos 

void setup() {
  size(405, 720);

  for (int i = 0; i < mov.length; i ++) {
    mov[i] = new Movie(this, "mov"+i+".mp4");
  }

  myMovie3 = new Movie(this, "background.mov"); // background video

  myMovie3.loop();
}

void movieEvent(Movie m) {
  m.read();
}

void draw() {
  background(0);
  rIndex = int(random(2, 8));
  gIndex = int(random(0, 2));
  if (state == IDLE) {
    image(myMovie3, 0, 0, width, height);
    textSize(26);
    text("Count Variable: " + count, 130, 50);
  } else if (state == ANIM1) {
    for (int i = 0; i < mov.length; i++) {
      image(mov[rIndex], 0, 0, width, height);
      if (mov[rIndex].time() >= 6.0) state = IDLE;
    }
  } else if (state == ANIM2) {
    for (int i = 0; i < mov.length; i++) {
      image(mov[gIndex], 0, 0, width, height);
      if (mov[gIndex].time() >= 10.0) state = IDLE;
    }
  }
}

void mousePressed() {
  if (mouseButton == LEFT) { 
    state = ANIM1;
    mov[rIndex].jump(0.0);
    mov[rIndex].play();

    count = count + 5;
  } else if (mouseButton == RIGHT) {
    state = ANIM2;
    mov[gIndex].jump(0.0);
    mov[gIndex].play();
    count -= 3;
  } else state = IDLE;
}

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 27, 2019, 1:02am UTC](https://discourse.processing.org/t/random-video-generated-for-every-mouse-click/14958/2 "2019-10-27T01:02:22Z")

</div>

-a- please format your code above,  
use the

```auto
</> button from the editor menu

```

looks like  
```  
type or paste code here  
```

* * *

on the first look i would say that a random movie to play  
with the random number made in ( every / 60 per sec ) draw loop  
your computer / OS will refuse…

move the index random generation to beginning of  
? mouse click ?

---

<div class="post-metadata">

**Author:** ![szb1](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@szb1](https://discourse.processing.org/u/szb1)\
**Post date:** [October 27, 2019, 1:15am UTC](https://discourse.processing.org/t/random-video-generated-for-every-mouse-click/14958/3 "2019-10-27T01:15:38Z")

</div>

Hi,  
Sorry I for got about formatting it. Thanks for the help. Its working now! quick question though

for this line of code

```auto
rIndex = int(random(2, 8));
gIndex = int(random(0, 2));

```

because my videos are stored in an array for rIndex when I say generate a random number from 2,8 does this mean that in my array if the number generated was 3 then the whatever video at 3 will be played? and also because my array has 8 items, if I set it to 8 it will mean I will not get 8 as 8 will be exclusive for the random function?

thanks!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 27, 2019, 1:20am UTC](https://discourse.processing.org/t/random-video-generated-for-every-mouse-click/14958/4 "2019-10-27T01:20:35Z")

</div>

general idea DEBUG easy way  
`println("rIndex "+rIndex+" gIndex "+gIndex);`  
if you would have tested that inside draw of your first code, you might have seen how you torture the OS

* * *

> **[random() / Reference](https://processing.org/reference/random_.html)**
>
> Generates random numbers. Each time the random() function is called, it returns an unexpected value within the specified range. If only one parameter is passed to the function, it will return a…

`(int)random(2,8)`

> 2 3 4 5 6 7 NOT 8

---

<div class="post-metadata">

**Author:** ![szb1](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@szb1](https://discourse.processing.org/u/szb1)\
**Post date:** [October 27, 2019, 1:45am UTC](https://discourse.processing.org/t/random-video-generated-for-every-mouse-click/14958/5 "2019-10-27T01:45:36Z")

</div>

ok I was just confirming. So if you notice that my Movie array has 8 and so for the rIndex I want 8 to be inclusive but what happens is when I write

```auto
rIndex = int(random(2, 9));

```

I get the ArrayIndexOutOfBoundsException:8 error…

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [October 27, 2019, 1:46am UTC](https://discourse.processing.org/t/random-video-generated-for-every-mouse-click/14958/6 "2019-10-27T01:46:38Z")

</div>

?? array[8] goes from 0,1,2,3,4,5,6,7  
so your code was GOOD,

---

<div class="post-metadata">

**Author:** ![szb1](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@szb1](https://discourse.processing.org/u/szb1)\
**Post date:** [October 27, 2019, 1:51am UTC](https://discourse.processing.org/t/random-video-generated-for-every-mouse-click/14958/7 "2019-10-27T01:51:12Z")

</div>

Hi yeah I forgot it started from 0, 😅 Thanks heaps! but I changed it to being

```auto
rIndex = int(random(2, mov.length));

```
