Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Random slection of item in a variable length array


Message #1 by "Tim Trepanier" <ttrepanier@n...> on Mon, 10 Feb 2003 13:46:18
With a fresh mind today I got it working: Here's my randomizing code:

var showlength=showslide.length
var currentslide=Math.floor(Math.random()*showlength)

I knew it was simple. Don't know why i didn't think of this earlier.

Thanks


> I have an array that i'm trying to get the length of then randomly 
select 
o> ne of it's items. The array isn't always the same length so i need to 
c> heck it's length before getting a random number within that range. 
S> ounds simple, but it's giving me a headache. Please help me get this 
w> orking.

> After trying various things here's a sample of my array and randomizing 
c> ode:
s> howslide[0]=['parameter', 'goes', 'here']
s> howslide[1]=['some', 'are', 'optional']
s> howslide[2]=['text', '', '']

> var showlength=0
v> ar currentslide=0
s> howslide[showslide.length]='showlength'
c> urrentslide=Math.floor(Math.random()*'showlength' )

> Thank You


  Return to Index