Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 24th, 2012  |  syntax: None  |  size: 1.62 KB  |  hits: 9  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Random Number with javascript or jquery
  2. $(document).ready(function () {
  3.     abc();
  4.     test = array();
  5.  
  6.     function abc() {
  7.         res = randomXToY(1, 10, 0);
  8.  
  9.         $('#img' + res).fadeTo(1200, 1);
  10.         //$(this).addClass('activeImg');
  11.         //});
  12.         setTimeout(function () {
  13.             removeClassImg(res)
  14.         }, 3000);
  15.     }
  16.  
  17.     function removeClassImg(res) {
  18.         $('#img' + res).fadeTo(1200, 0.1);
  19.         //$('#img' + res).removeClass('activeImg');
  20.         abc();
  21.     }
  22.  
  23.     function randomXToY(minVal, maxVal, floatVal) {
  24.         var randVal = minVal + (Math.random() * (maxVal - minVal));
  25.  
  26.         return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal);
  27.  
  28.     }
  29. });
  30.        
  31. function Randomizer(minVal, maxVal, floatVal){
  32.     var possible_results = []; // for larger arrays you can build this using a loop of course
  33.     var randomization_array = [];
  34.     var count = minVal;
  35.     var incrementor = floatVal || 1; // set the distance between possible values (if floatVal equals 0 we round to 1)
  36.     while (count <= maxVal) {
  37.         possible_results.push(count);
  38.         count += incrementor;
  39.     }
  40.  
  41.     this.run = function(){
  42.         // if randomization_array is empty set posssible results into it
  43.         randomization_array = randomization_array.length ? randomization_array : $.merge(randomization_array, possible_results);
  44.         // pick a random element within the array
  45.         var rand = Math.floor(Math.random()*randomization_array.length);
  46.         // return the relevant element
  47.         return randomization_array.splice(rand,1)[0];  
  48.     }
  49. }
  50.        
  51. rand = new Randomizer(1,10,0);
  52. rand.run();