see http://jsfiddle.net/JPxXp/
I understand as to why I keep getting 1 as the minimum ball number; do I create another variable / array that holds the 6 randomised numbers (if so, how - I don't fully understand how to do this).
see http://jsfiddle.net/JPxXp/
I understand as to why I keep getting 1 as the minimum ball number; do I create another variable / array that holds the 6 randomised numbers (if so, how - I don't fully understand how to do this).
First, define an array that holds the randomised numbers:
var random_balls = [];
Each time you generate a random number, add it to that array:
// generate, the + is to convert a string like '12' to the number 12, otherwise
// comparing them gives wrong results: '12' < '9' but 12 > 9
var random_ball = +balls[Math.floor(Math.random()*balls.length)];
// if indexOf returns -1, the ball is not present in the array
// (otherwise it returns the index at which the ball is)
while(random_balls.indexOf(random_ball) !== -1) {
// generate again if you already took this ball
random_ball = +balls[Math.floor(Math.random()*balls.length)];
}
// add to array
random_balls.push(random_ball);
// display
document.write(random_ball);
The min checker would iterate the new array with random numbers like this:
for (i=0; i<random_balls.length; i++)
{ if (random_balls[i] < min)
min = random_balls[i];
}
In case you didn't know, you can also use:
var min = Math.min.apply(null, random_balls); // use built in min function, passing array as arguments
Define global min variable and change it after selecting random ball:
var min = Number.POSITIVE_INFINITY;
...
for(i=0; i<NUMBER_OF_BALLS; i++) {
var ball = balls[Math.floor(Math.random()*balls.length)];
min = Math.min(ball, min);
document.write(ball);
document.write(" ");
}
fiddle: http://jsfiddle.net/Daess/JPxXp/4/
After the loop min
variable will hold min number.
Indeed you need to push the values that you randomly choose into an array, and find the min WRT to that array.
document.write("your balls are ");
NUMBER_OF_BALLS = 6
var i = 0;
var balls = ['1','2','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31','31','32','33','34','35','36','37','39','40'];
var chosen = [];
for(i=0; i<NUMBER_OF_BALLS; i++) {
chosen.push(balls[Math.floor(Math.random()*balls.length)]);
document.write(chosen[chosen.length-1]);
document.write(" ");
}
document.write("<br>");
document.write("<br>");
min = Number.POSITIVE_INFINITY;
for (i=0; i<chosen.length; i++) {
if (chosen[i] < min)
min = chosen[i];
}
document.write("The lowest ball was: " + min);
document.write("<br>");
You're taking the minimum from the balls
array. All you're doing is printing out six random elements from the array. You'll need to create a new array with 6 elements, copy 6 random ones from the original array into that, then get the minimum. Also mind that you could get the same element twice.
I'm going to go for a different way here:
var arr = [], // acceptable values
randomArray = []; // random array
// Create an array of numbers 0 to 45;
for(var i=1; i <=45 ; i++){
arr.push(i);
}
// From http://stackoverflow.com/q/962802#962890
function shuffle(array) {
var tmp, current, top = array.length;
if(top) while(--top) {
current = Math.floor(Math.random() * (top + 1));
tmp = array[current];
array[current] = array[top];
array[top] = tmp;
}
return array;
}
// Randomly sort the array and take 6
randomArray = shuffle(arr).slice(0,6);
document.write("Random array: " + randomArray + "<br>");
document.write("Minimum value: " + Math.min.apply(Math, randomArray));
Example: http://jsfiddle.net/jonathon/85vEA/
At the start, I'm generating my set of numbers from 1-45 (instead of the string array you have). I'm then sorting it randomly using the Fisher-Yates shuffle (see the link to the other question) and, to get my subset of 6 random numbers, I do a slice on the array.
The minimum value can then be found by doing Math.min.apply(Math, randomArray)