Random number between two numbers

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?

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);   
}
1 Like

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

1 Like

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

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

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

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

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);  
}

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

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

2 Likes

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

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 =))

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

//... 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)

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

@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:

// 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

1 Like

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.