handling random values

Hi,

Please see my code below:

I only want to generate random values 0-2 however it goes up to 10.
Can someone please advise how I should adjust my random function.

thanks


var values = new Array();

values[0]="hello1";
values[1]="hello2";
values[2]="hello3";
values[3]="hello4";

while (confirm("again?"))
{
      document.write("<pre>");
      document.writeln(values[Math.floor(Math.random()*10)]);
      document.write("</pre>");
}
jecommeraAsked:
Who is Participating?
 
CluskittConnect With a Mentor Commented:
Change Math.random()*10 with Math.random()*2
0
 
COBOLdinosaurCommented:
while (confirm("again?"))
{
      document.write("<pre>");
      document.writeln(values[Math.floor(Math.random()*2)]);
      document.write("</pre>");
}

Open in new window



Cd&
0
 
COBOLdinosaurCommented:
Sorry Cluskitt, I didn't see your post


Cd&
0
 
CluskittCommented:
No problems, you posted within seconds of me, it happens all the time. As long as there is a solution, that's all that matters.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.