There is a code that sets the change of pictures:

var image_count = 7; var interval = 16000; var time_out = 5; var i = 0; var timeout; var opacity = 100; function change_image() { opacity--; var j = i + 1; var current_image = 'img_' + i; if (i == image_count) j = 1; var next_image = 'img_' + j; document.getElementById(current_image).style.opacity = opacity / 100; document.getElementById(current_image).style.filter = 'alpha(opacity=' + opacity + ')'; document.getElementById(next_image).style.opacity = (100 - opacity) / 100; document.getElementById(next_image).style.filter = 'alpha(opacity=' + (100 - opacity) + ')'; timeout = setTimeout('change_image()', time_out); if (opacity == 1) { opacity = 100; clearTimeout(timeout); } } setInterval(function () { i++; if (i > image_count) i = 1; change_image(); }, interval); 

Instead of stitching:

 var i = 0; 

I added:

 var i = Math.random(); function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } 

But for some reason it does not work. That is, when the variable i is zero, it takes the first image in the list and then changes it in a row. And I wanted him to take a random picture and then go in order in the list. Here is a list of pictures:

 <img src="/images/fon/70.jpg" id="img_1" style="position: absolute;"> <img src="/images/fon/50.jpg" id="img_2" style="opacity: 0; filter: alpha(opacity=0); position: absolute;"> <img src="/images/fon/49.jpg" id="img_3" style="opacity: 0; filter: alpha(opacity=0); position: absolute;"> <img src="/images/fon/55.jpg" id="img_4" style="opacity: 0; filter: alpha(opacity=0); position: absolute;"> <img src="/images/fon/56.jpg" id="img_5" style="opacity: 0; filter: alpha(opacity=0); position: absolute;"> <img src="/images/fon/77.jpg" id="img_6" style="opacity: 0; filter: alpha(opacity=0); position: absolute;"> <img src="/images/fon/88.jpg" id="img_7" style="opacity: 0; filter: alpha(opacity=0); position: absolute;"> 
  • Why do you assign Math.random() and not getRandomInt(0, 7) ? - Grundy
  • @ user250973 That is, a random number is not taken in the border from 0-7 yes? - Raz Galstyan
  • because it was so said in the directory: javascript.ru/math.random - user250973
  • I don’t know .. as a result, the pictures stopped changing at all .. so, I think it doesn’t work in principle - user250973

2 answers 2

 var i = getRandomInt(0, 7); function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } 
  • one
    why + 1 ? (2 characters needed ...) - Igor
  • By this logic, are numbers really in the range? - Raz Galstyan
  • @RazmikGalstyan yes, they will be - jsbin.com/jiqocamuri/edit?js,console - Zoltan Toth
  • @ZoltanToth yes, but the other one is smaller. - Raz Galstyan
  • one
    @RazmikGalstyan agree .. I just took the source code of the author and indicated how it should be used to work - Zoltan Toth

In order to take a random number in the selected range, you need to do this:

 Math.random()*(ba)+a 

You have a=0 , b=7 ;

That is, you should do this:

 var i = Math.random()*7; 

And so you will have whole numbers in a given range of you:

 Math.floor((Math.random() * 7)); 
  • 2
    Uncaught ReferenceError: math is not defined - Qwertiy
  • It should have been fixed with a capital letter. - Raz Galstyan
  • one
    Yeah. And why was the edit @Dmitry roll back. which fixed it? - Qwertiy
  • @Qwertiy because not only was it fixed there, much more is the same - Raz Galstyan