# Generating non-repeating random numbers

**URL:** <https://forum.kirupa.com/t/generating-non-repeating-random-numbers/277069>\
**Category:** flash\
**Created:** [December 7, 2008, 11:37am UTC](https://forum.kirupa.com/t/generating-non-repeating-random-numbers/277069 "2008-12-07T11:37:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Toadstool13](https://avatars.discourse-cdn.com/v4/letter/t/edb3f5/32.png) [@Toadstool13](https://forum.kirupa.com/u/Toadstool13)\
**Post date:** [December 7, 2008, 11:37am UTC](https://forum.kirupa.com/t/generating-non-repeating-random-numbers/277069/1 "2008-12-07T11:37:56Z")

</div>

I am trying to create a quiz.  
I have created arrays to hold the questions and answers

I have generated the first random number (randomQ) and stored it along with the first generated question and answer. I want to generate the rest of the random numbers. I know the code is flawed, can someone put me right? At the moment the questions are limited to seven, hence the random number limitation.

while(q \< questionTest.length-2) // I have already generated one number  
{  
while (randValue.length \< questionTest.length)  
{ q++; trace("q = " +q);  
//generate and test new random number  
randomQ2 = Math.floor((Math.random()\*7) + 0);  
for (var r:int=0; r\<randValue.length;r++)  
{  
if (randomQ2 == randValue[r])  
{ randomQ2 = Math.floor((Math.random()\*7) + 0);}  
if (randomQ2 == randValue[r+1])  
{ randomQ2 = Math.floor((Math.random()\*7) + 0);}  
if (!(randomQ2 == randValue[r]))  
{ randomQ = randomQ2; }  
}  
randValue.push(randomQ);  
}  
}

The final output this time was =\> randValue = 3,1,5,4,5,0,6

I get one duplication and one missing number, in this case “2” is missing and “5” repeats.

👁
