Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Unique Random Number Problem

Status
Not open for further replies.

jgd12345

Technical User
Apr 22, 2003
124
GB
Hi I have the following script which should generate 3 unique random numbers and store them in the corresponding text boxes when run but it returns an error.

<script>
function addLuckyDip(lineNumber) {
var randomnumber[0] = Math.floor(Math.random() * maxValue) + 1;
var randomnumber[1] = randomnumber[0];
var randomnumber[2] = randomnumber[0];

while(randomnumber[1] == randomnumber[0]) {
randomnumber[1] = Math.floor(Math.random() * maxValue) + 1;
}

while(randomnumber[2] == randomnumber[0] || randomnumber[2] == randomnumber[1]) {
randomnumber[2] = Math.floor(Math.random() * maxValue) + 1;
}

for(var i = 0; i < 3; i++) {
document.lotteryPlayslipForm.elements["drawGameBoards["+lineNumber+"][numbersChosen]["+i+"]"].value = randomnumber;
}
}
</script>

<form name="lotteryPlayslipForm">
<input type="text" name="drawGameBoards[0][numbersChosen][0]" class="text">
<input type="text" name="drawGameBoards[0][numbersChosen][1]" class="text">
<input type="text" name="drawGameBoards[0][numbersChosen][2]" class="text">
</form>
 
Looks everything OK, except for array initialization. Try this:

Code:
[b]var randomnumber = new Array();[/b]
   randomnumber[0] = Math.floor(Math.random() * maxValue) + 1;
   randomnumber[1] = randomnumber[0];
   randomnumber[2] = randomnumber[0];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top