Bruce
Posts: 0
Joined: Sat Jul 26, 2014 7:59 pm

Pseudo Random Number Generator // image display

The goal is to have around 100 or so images, and every time a timer runs out, a different image is displayed. What I am looking for is code that displays an image at random (psuedo random?). I think using the appery Media Manager would be my best option. Can someone let me know how I would go about doing this/what would the code be for displaying an image at random from the media manager? Thanks in advance.

Alena Prykhodko
Posts: 0
Joined: Tue Apr 09, 2013 7:36 am

Pseudo Random Number Generator // image display

Hello Bruce,

The simplest way is the next:

1) Name all images, which you want to retrieve from Media Manager with one pattern, for example:
rand_img_XX.jpg
where instead of XX will be set random number;

2) Generate random number with codeMath.random() /code
(https://developer.mozilla.org/en-US/d...)
pre
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}

var randNum = getRandomInt(0,100);/pre

3) Into Image component with name imageName write our random image name:
pre
var imageUrl = Appery.getImagePath("rand_img_" + randNum + ".jpg");
Apperyio("imageName").attr("src", imageUrl);/pre

Bruce
Posts: 0
Joined: Sat Jul 26, 2014 7:59 pm

Pseudo Random Number Generator // image display

awesome!! Thank you for the quick and detailed response!

Makes sense - i'll give it a try and let you know if I have any issues

Return to “Issues”