Page 1 of 1

Pseudo Random Number Generator // image display

Posted: Sat Jul 26, 2014 7:59 pm
by Bruce

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.


Pseudo Random Number Generator // image display

Posted: Sun Jul 27, 2014 7:39 pm
by Alena Prykhodko

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


Pseudo Random Number Generator // image display

Posted: Mon Jul 28, 2014 1:29 am
by Bruce

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