random numbers

  • wiggles
  • Novice
  • Novice
  • wiggles
  • Posts: 15

Post 3+ Months Ago

i saw someone posted about random images and i need similar help. i need to generate a simple list of random numbers 1-9, but after a number is called i need to eliminate that number from being called again. this is for pics i am calling dynamically to run in a scrolling script i wrote and i would like for no image to repeat. ideas?
-jason
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Abelius
  • Proficient
  • Proficient
  • User avatar
  • Posts: 260
  • Loc: Miami Beach, FL, USA

Post 3+ Months Ago

Code: [ Select ]
function showNumbers() {
    tenNumbers = new Array("n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9", "n10");
    selectedNumbers = new Array();
    for (var j = 0; j < 10; ++j) {
        index = Math.floor(Math.random() * tenNumbers.length);
        selectedNumbers.push(tenNumbers[index]);
        tenNumbers.splice(index, 1);
    }
    _root.r_txt.text = selectedNumbers.join(", ");
    _root.cards_txt.text = selectedNumbers.slice(0, 5);
}
  1. function showNumbers() {
  2.     tenNumbers = new Array("n1", "n2", "n3", "n4", "n5", "n6", "n7", "n8", "n9", "n10");
  3.     selectedNumbers = new Array();
  4.     for (var j = 0; j < 10; ++j) {
  5.         index = Math.floor(Math.random() * tenNumbers.length);
  6.         selectedNumbers.push(tenNumbers[index]);
  7.         tenNumbers.splice(index, 1);
  8.     }
  9.     _root.r_txt.text = selectedNumbers.join(", ");
  10.     _root.cards_txt.text = selectedNumbers.slice(0, 5);
  11. }


Have a dynamic text field and give it an instance name "r_txt"...

Hope it helps... :)
  • wiggles
  • Novice
  • Novice
  • wiggles
  • Posts: 15

Post 3+ Months Ago

so how would i use this? i understand the basic idea, i just dont see how i would call the pics. i simply want the program to pick a number between 1 and 10, call that pic, wait and redo. all the while remembering what numbers have been called and repeat when its all over.
-jason
ps-thanks for posting.
  • wiggles
  • Novice
  • Novice
  • wiggles
  • Posts: 15

Post 3+ Months Ago

ive been pouring over your code and i understand the functionality of all of it except the line
Code: [ Select ]
  _root.r_txt.text = selectedNumbers.join(", ");
  _root.cards_txt.text = selectedNumbers.slice(0, 5);
  1.   _root.r_txt.text = selectedNumbers.join(", ");
  2.   _root.cards_txt.text = selectedNumbers.slice(0, 5);

can you explain what this does?
-jason
  • urbansquall
  • Born
  • Born
  • urbansquall
  • Posts: 1

Post 3+ Months Ago

jason,

Simple do. If you want an array of random n numbers from 1 to n, just try something like this

Code: [ Select ]
function getRandomArrayOfNumbers( n: Number ) : Array
{
  var returnArray: Array = new Array();
  for( var i = 0; i < n; i++ )
  {
    returnArray.push(i);
  }
   /// returnArraylooks like 1,2,3,4,5, etc. right now
  for( var i = 0; i < n; i++ )
  {
    var swapIndex : Number = Math.floor(Math.random() * n);
    var currentValue : Number = returnArray[i];
    returnArray[i] = returnArray[swapIndex];
    returnArray[swapIndex] = currentValue;
  }

  // returnArray looks like 4,2,3,7,1 etc.
  return returnArray;
}
  1. function getRandomArrayOfNumbers( n: Number ) : Array
  2. {
  3.   var returnArray: Array = new Array();
  4.   for( var i = 0; i < n; i++ )
  5.   {
  6.     returnArray.push(i);
  7.   }
  8.    /// returnArraylooks like 1,2,3,4,5, etc. right now
  9.   for( var i = 0; i < n; i++ )
  10.   {
  11.     var swapIndex : Number = Math.floor(Math.random() * n);
  12.     var currentValue : Number = returnArray[i];
  13.     returnArray[i] = returnArray[swapIndex];
  14.     returnArray[swapIndex] = currentValue;
  15.   }
  16.   // returnArray looks like 4,2,3,7,1 etc.
  17.   return returnArray;
  18. }


Just coded that now. Hope it works.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 16 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.