I can't figure out how to make a random number generator in javascript that won't have duplicates, but will restart once it gets to no digits left. Any advice you can give me would be greatly appreciated, Thanks.
Random number generator without duplicates?
Page 1 of 13 Replies - 1214 Views - Last Post: 25 April 2012 - 07:21 PM
Replies To: Random number generator without duplicates?
#2
Re: Random number generator without duplicates?
Posted 24 April 2012 - 08:10 PM
Create a sequential array for the length you need.
For example: 0,1,2,3,4,5,6,7,8,9
Then randomly sort that sequential array
For example, results could be: 5,0,9,2,6,1,7,4,8,3
Finally, pull from the random array in sequential order
until you reach the end of the array.
Repeat final step as required. No repeats until array is re-used.
For example: 0,1,2,3,4,5,6,7,8,9
Then randomly sort that sequential array
For example, results could be: 5,0,9,2,6,1,7,4,8,3
Finally, pull from the random array in sequential order
until you reach the end of the array.
Repeat final step as required. No repeats until array is re-used.
#3
Re: Random number generator without duplicates?
Posted 25 April 2012 - 04:19 AM
i need it to be 0-9 (10 different numbers), and for example a result would be 3 7 6 5 2 8 1 9 0 4, 5 8 3 4 7 1 9 0 2 6, after the first four it repeated, but i would need it to be able to recall the results i got from many numbers ago, if it makes a difference im using the numbers to choose slides randomly in a slide show and the user may get to slide 16 and for some reason want to go look at slide 2 again.
#4
Re: Random number generator without duplicates?
Posted 25 April 2012 - 07:21 PM
Johnlang1993, on 25 April 2012 - 05:19 AM, said:
i need it to be 0-9 (10 different numbers), and for example a result would be 3 7 6 5 2 8 1 9 0 4, 5 8 3 4 7 1 9 0 2 6, after the first four it repeated, but i would need it to be able to recall the results i got from many numbers ago, if it makes a difference im using the numbers to choose slides randomly in a slide show and the user may get to slide 16 and for some reason want to go look at slide 2 again.
I'm a bit confused. Do you have any code to show what you are doing?
Also, if you need 10 different numbers, where is slide #16 coming from?
If you have only 10 slides, there must be a repeat if you display 16 times. (???)
Afterthought...
Here is some testing code which assumes 20 slides to be displayed (without the slides)
<!DOCTYPE html>
<html>
<head>
<title> Untitled </title>
<script type="text/javascript">
// For:
var SlideMax = 20;
var Slides = [];
for (var i=0; i<SlideMax; i++) { Slides[i] = i; }
function rndOrder() { return (Math.round(Math.random())-0.5); }
window.onload = function() {
var str = 'Array: '+Slides.join(', ')+'<br>';
Slides.sort(rndOrder);
str +='Value: '+Slides.join(', ')+'<br>';
document.getElementById('slideOrder').innerHTML = str;
}
function showSlide(info) {
var SlidePtr = Number(info);
alert('Slide# '+(SlidePtr+1)+' is array position: '+SlidePtr+' with value of: '+Slides[SlidePtr]);
}
</script>
</head>
<body>
<pre>
<div id="slideOrder"></div>
<p>
<script type="text/javascript">
var str = 'Slide: ';
for (var i=0; i<SlideMax; i++) {
str += '<label style="display:block; float:left; width:75px">';
str += '<input type="radio" name="RBtn" value="'+i+'"';
str += ' onclick="showSlide(this.value)"> '+(i+1)+' ';
str += '</label>';
if ((i % 10) == 9) { str += '<br style="clear:both"> '; }
}
document.write(str);
</script>
</pre>
</body>
</html>
This post has been edited by JMRKER: 25 April 2012 - 08:12 PM
Page 1 of 1
|
|

New Topic/Question
Reply


MultiQuote




|