1

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).

geef
  • 589
  • 1
  • 6
  • 8
  • 1
    possible duplicate of [How to get the min elements inside an array in javascript?](http://stackoverflow.com/questions/5442109/how-to-get-the-min-elements-inside-an-array-in-javascript) – nikc.org Aug 23 '11 at 11:23
  • Why isn't `1` an acceptable minimum for `['1','2','4',...,'40']`? – Álvaro González Aug 23 '11 at 11:23
  • 1
    Commenters above haven't read the question (although it's written not very clearly). The problem is not finding the lowest value, but to generate an array of random numbers. – GolezTrol Aug 23 '11 at 11:29
  • @Alvaro G. Vicario I am after the minimum value of the six randomised numbers, obviously 1 would be an acceptable minimum value if I was after the minimum value of the 40 balls, but this is not what I was after. – geef Aug 23 '11 at 11:31
  • 1
    If you think your question isn't written very clearly, perhaps you should fix that. – nikc.org Aug 23 '11 at 11:35
  • 1
    Can your randomized set contain duplicate numbers or should they be unique? – nikc.org Aug 23 '11 at 11:38
  • @chaad - Alright... Now I 't understand the question. You already accepted a (nice) answer but your logic was fine as well: you simply made the check in the wrong loop. – Álvaro González Aug 23 '11 at 11:46

5 Answers5

2

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

http://jsfiddle.net/pimvdb/JPxXp/7/

pimvdb
  • 151,816
  • 78
  • 307
  • 352
  • 1
    @DaveRandom: It seems to work for me. What browser are you using? – pimvdb Aug 23 '11 at 11:31
  • IE8, and looking at the code I can;t figure out why it doesn't, but it just doesn't - gave me this: `your balls are 5 35 18 15 40 35 The lowest ball was: 15` and it should be 5... – DaveRandom Aug 23 '11 at 11:34
  • @DaveRandom: I missed something - comparing strings rather than numbers. – pimvdb Aug 23 '11 at 11:36
  • yeh I just clocked that too, needs a `parseInt()` - everyone seems to have missed that one! – DaveRandom Aug 23 '11 at 11:38
  • 1
    If you are modifying it, I suspect it would be an idea to add something that ensures drawn values are not repeated, since this looks like a lotto-type simulation, and you can't get the same lotto ball twice... – DaveRandom Aug 23 '11 at 11:41
1

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.

Igor Dymov
  • 16,230
  • 5
  • 50
  • 56
1

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>");
Itay Maman
  • 30,277
  • 10
  • 88
  • 118
0

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.

G_H
  • 11,739
  • 3
  • 38
  • 82
0

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)

Jonathon Bolster
  • 15,811
  • 3
  • 43
  • 46