# Thread: Random Numbers, Not Consecutive

1. ## Random Numbers, Not Consecutive

Hi,

I am looking for some assistance with modifying some javascript code I found. Currently the code produces 2 different random numbers but I need three.

For example, it should give me a readout like 0, 2, 1 but NOT 0, 2, 0 or 1,1,2.

Can anyone help me?

Code:
```<script language="JavaScript">
<!-- Hide this script from old browsers --

var randomNum1=Math.floor(Math.random()*2);
var randomNum2=randomNum1;
while (randomNum2 == randomNum1){
randomNum2=Math.floor(Math.random()*2);
}

alert(randomNum1 + ' and ' + randomNum2);
</script>```

2. Populate an array with consecutive numbers then sort it randomly.
Code:
```var numbers = [1, 2, 3];
numbers.sort(function(){return Math.random() - 0.5;});

3. A standard way is to create an empty array and raise a flag for each value that you're already picked.

Code javascript:
```function randomNumber(limit) {
return Math.floor(Math.random() * limit);
}
var selectedNumbers = [],
limit = 3,
number = 0,
result = '',
i;
for (i = 0; i < limit; i += 1) {
number = randomNumber(limit);
while (selectedNumbers[number] === true) {
number = randomNumber(limit);
}
selectedNumbers[number] = true;
if (result > '') {
result += ' and ';
}
result += number;
}

4. Wow! Thanks! Both these solutions get me one step closer but I have a follow-up question.

I need to use the numbers later on in my html document. How to I separate the numbers into different variables?

Thanks again!

5. The array method already stores the values as numbers[0], numbers[1], etc. If necessary you can assign them to other variables:
Code:
```var myVarName = numbers[0];
var myVarForLater = numbers[1];```

6. If you store the result string somewhere you can use
resultArray = result.split(' and ') to end up with an indexable array of the numbers.

7. I'm starting to doubt my method now! I'm not convinced it will give a uniform distribution, it depends on the sorting algorithm. The random number in the sort function may have to be tweaked.

8. So, here's a new method.
Code:
```var numbers = [1, 2, 3], sorted = [];
while (numbers.length){
sorted.push(numbers.splice(Math.random() * numbers.length, 1));
}
It seems to work without Math.floor but you may want to add it in. This time it does produce a uniform distribution.

9. The person who posted the above code later on said that it doesn't do a good job, they're not very random at all, and came up with a much better way to randomise them.
http://www.sitepoint.com/forums/show...40&postcount=8

Working on from there, here is how I suggest that you achieve what you're after.

Code javascript:
```var videos = [
{'index': 1, 'desc': 'This is the text linked to number 1'},
{'index': 2, 'desc': 'This is the text linked to number 2'},
{'index': 3, 'desc': 'This is the text linked to number 3'},
{'index': 4, 'desc': 'This is the text linked to number 4'},
{'index': 5, 'desc': 'This is the text linked to number 5'},
{'index': 6, 'desc': 'This is the text linked to number 6'},
{'index': 7, 'desc': 'This is the text linked to number 7'},
{'index': 8, 'desc': 'This is the text linked to number 8'},
{'index': 9, 'desc': 'This is the text linked to number 9'},
{'index': 10, 'desc': 'This is the text linked to number 10'},
{'index': 11, 'desc': 'This is the text linked to number 11'}
];
randomised = [];
while (videos.length) {
randomised.push(videos.splice(Math.random() * videos.length, 1));
}
var item1 = randomised[0].index;
var item2 = randomised[1].index;
var item3 = randomised[2].index;```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•