# Random number between two numbers

**URL:** https://discourse.processing.org/t/random-number-between-two-numbers/1017
**Category:** Processing
**Created:** [June 16, 2018, 8:23pm UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017 "2018-06-16T20:23:13Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![janberkjk](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@janberkjk](https://discourse.processing.org/u/janberkjk)
#### Post date: [June 16, 2018, 8:23pm UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017/1 "2018-06-16T20:23:13Z")

</div>

Hello people,

I am trying to get a nuber between two numbers, but with the poor code i write so far i get a random number between 1 and 10, but i want bgfoto between 7 and 10, can you help me please?

```auto
PImage foto;
PImage bg;
int fotoname;
int bgfoto;
 
void setup(){
  size(1080,680);  
  bgfoto = int(random(7,10));
  fotoname = (5);
  frameRate(8);
}

void draw(){
  println(frameRate);
  image(loadImage (int(random(bgfoto)) + ".jpg"), 0, 0, 1080, 680);
  image(loadImage (int(random(fotoname)) + ".jpg"), 200, 230,180,180);   
}

```

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [June 16, 2018, 8:38pm UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017/2 "2018-06-16T20:38:15Z")

</div>

`int randomNumber = 7 + int( random( 4 ) );`

---

<div class="post-metadata">

### Author: ![janberkjk](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@janberkjk](https://discourse.processing.org/u/janberkjk)
#### Post date: [June 17, 2018, 9:01am UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017/3 "2018-06-17T09:01:19Z")

</div>

Thank you very very much one another step is done =)

---

<div class="post-metadata">

### Author: ![codingking03](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@codingking03](https://discourse.processing.org/u/codingking03)
#### Post date: [June 17, 2018, 11:12am UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017/4 "2018-06-17T11:12:19Z")

</div>

random can have two arguments just high, or if you have two, low and high:

```auto
float r1 = random(4); //r1 is a float between 0 and 4
float r2 = random(7, 11); // r2 is a float between 7 and 10

int num1 = round(r1); //rounds to integer
int num2 = round(r2); //rounds to integer

```

use num1 and num2 from this point if you need integers

---

<div class="post-metadata">

### Author: ![janberkjk](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@janberkjk](https://discourse.processing.org/u/janberkjk)
#### Post date: [June 17, 2018, 11:22am UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017/5 "2018-06-17T11:22:30Z")

</div>

Thank you for your answer, but if you want to prove that you are the real king of the coding ( =) ) can you explane me how i can change framerate? i want two different framerates for two different images, and i watch to many lessons and i read half of a book but i couldnt understand of course because i am very new in this. i simplified the code since it looks works.

i need first foto frameRate is 1,  
and second foto frameRate is 8, or so  
btw i will have total 9 different foto group in the project

```auto
void setup() {
  size(1080,680); 
  frameRate(1); 
}

void draw() {
  println(frameRate);
  image(loadImage (7 + int( random( 3 ) ) + ".jpg"), 0, 0, 1080, 680);
  image(loadImage (int(random(5)) + ".jpg"), 200, 230,180,180);  
}

```

---

<div class="post-metadata">

### Author: ![codingking03](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@codingking03](https://discourse.processing.org/u/codingking03)
#### Post date: [June 17, 2018, 11:24am UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017/6 "2018-06-17T11:24:44Z")

</div>

frameRate() changes the speed of the sketch… what is in need of a frame rate? are you trying to run a gif?

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [June 17, 2018, 11:26am UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017/7 "2018-06-17T11:26:06Z")

</div>

> [@codingking03](#):
>
> float r2 = random(7, 10); // r2 is a float between 7 and 10

A small correction to @codingking03. The above line, if you use `int()` to get the integer part, you will see that the results are between 7 and 9. If you use `round()`, you will not get an entire random distribution between 7 and 10 but it will be biased toward 8,9. Why is that? When you round, you take a range from [-0.5… 0.5]. For instance, you get an 8 when the number is between 7.5 and 8.5. However, for 10 you only have 9.5 to 10.0 available. For 7, you only have the range from 7.0 to 7.5 and this results in the probability of these two number to be lower aka. bias.

You should use Thomas’ solution or use `int(random(7,11))`

Kf

---

<div class="post-metadata">

### Author: ![codingking03](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@codingking03](https://discourse.processing.org/u/codingking03)
#### Post date: [June 17, 2018, 11:27am UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017/8 "2018-06-17T11:27:38Z")

</div>

Thanks! should I edit mine or leave it for yours to be seen?

---

<div class="post-metadata">

### Author: ![janberkjk](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@janberkjk](https://discourse.processing.org/u/janberkjk)
#### Post date: [June 17, 2018, 11:51am UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017/9 "2018-06-17T11:51:24Z")

</div>

Thank you for all the answers. What i want to see is;

randomly selected pictures in the background with a slow framerate  
and randomly selected pictures (different pictures than bg fotos) on it with a faster framerate

i will have hundreds of pictures for this project,  
background will be slowly change and other images will change faster  
at least i will try =))

---

<div class="post-metadata">

### Author: ![codingking03](https://avatars.discourse-cdn.com/v4/letter/c/3ec8ea/32.png) [@codingking03](https://discourse.processing.org/u/codingking03)
#### Post date: [June 17, 2018, 12:04pm UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017/10 "2018-06-17T12:04:42Z")

</div>

I could do this in a couple minutes with p5.js but im shifting to processing…

you could have an array of strings. then a function that based on a string argument would change an image (all of them loaded) at an “x,y,width,height” location. then use an if statement

```auto
//... above code

void draw() {
r = random(0,allPics.length+1); //thanks to kfrajer for the +1

if(timer>100){
drawImage(allPics[r]);
timer=0;
}

timer++; //increases by 1 per frame framerate() changes how many frames per second
};

```

that code would require some pretense (variables and functions but in theory the method should work)

---

<div class="post-metadata">

### Author: ![kfrajer](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kfrajer/32/196_2.png) [@kfrajer](https://discourse.processing.org/u/kfrajer)
#### Post date: [June 17, 2018, 2:13pm UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017/11 "2018-06-17T14:13:28Z")

</div>

@codingking03 Maybe correct yours if you wish. I have your comment capture in a quote so it should be clear.

> [@janberkjk](#):
>
> image(loadImage (int(random(bgfoto)) + “.jpg”), 0, 0, 1080, 680);

@janberkjk How many pictures are you managing. It is preferable to load the images in setup. If you are working with lots of images, you need to build a better strategy than loading images in draw. Related to the fast and slow changing rate, can you provide some numbers? Like slow rate would be changing the background every 10 seconds? Last point, do not use `framerate(1)` in setup. Instead, you can manage everything related to rate in `draw()`. You can use the example provided by @codingking03. Another concept is to use the modulus operator. Yu can check about it in the reference. Here is some demo how it could be used in your case:

```java
// This is where you have all the images. I am assuming your background 
// and show images are coming from the same pool of images
PImage[] imageStack;   

void setup() {
  ...
}

void draw() {

  //Slow rate changes
  if (frameCount%300==0) { //About every 10 seconds if running at 30fps
    background(getMyImage());
  }

  //Fast rate change
  if (frameCount%15==0) { //About every half second if running at 30fps
    image(getMyImage(), width/2, height/2);
  }
}

PImage getMyImage() {
  return imageStack[int(random(imageStack.length))];
}

```

Kf

---

<div class="post-metadata">

### Author: ![janberkjk](https://avatars.discourse-cdn.com/v4/letter/j/e480ec/32.png) [@janberkjk](https://discourse.processing.org/u/janberkjk)
#### Post date: [June 17, 2018, 2:24pm UTC](https://discourse.processing.org/t/random-number-between-two-numbers/1017/12 "2018-06-17T14:24:01Z")

</div>

Hello, I will try to understand and use your advices, thank you very much

I will have around 52 pictures for background and i think bg picture will change in 8 seconds  
and at the near of the center of the screen there will be 8 different pictures,  
all randomly selected and for all those 8 pictures there will be 100 images, so 800 images in total for the center and they will change very quikly like 8 frames per second.
