0
var swf=["1.swf","2.swf","3.swf"];
var i = Math.floor(Math.random()*swf.length);

alert(swf[i]); // swf[1]  >> 2.swf

This case ,Random output One number.

How to Random output two different numbers ?

2
  • 3
    Please clarify what you're attempting to accomplish. If you need two random numbers, you can just execute the same code again. No? Commented Dec 2, 2010 at 2:35
  • I want 2 different number swf Commented Dec 2, 2010 at 2:41

4 Answers 4

4
var swf = ['1.swf', '2.swf', '3.swf'],

// shuffle
swf = swf.sort(function () { return Math.floor(Math.random() * 3) - 1; });

// use swf[0]
// use swf[1]

Even though the above should work fine, for academical correctness and highest performance and compatibility, you may want to shuffle like this instead:

var n = swf.length;
for(var i = n - 1; i > 0; i--) {
    var j = Math.floor(Math.random() * (i + 1));
    var tmp = swf[i];
    swf[i] = swf[j];
    swf[j] = tmp;
}

Credits to tvanfosson and Fisher/Yates. :)

Sign up to request clarification or add additional context in comments.

8 Comments

Maybe I am missing something, but should that 3 be this.length ?
@alex Nope. It's just randomly returning -1, 0 or 1 to randomly sort the array. developer.mozilla.org/en/JavaScript/Reference/Global_Objects/…
@deceze Ah, got it :) Thanks for the clarification. It's very clean too, so +1
I would typically expect the comparison function for sort to be deterministic. Having it not be deterministic means you run the risk of having your sort not terminate -- since it may decide to keep swapping two entries, though in practice I doubt it would happen. It would be better to use something like Fisher-Yates (bit.ly/eR3325) to shuffle which doesn't have this problem.
@tvanosson Interesting. I for one hadn't considered that.
|
2

You can use splice to remove the chosen element, then simply select another randomly. The following leaves the original array intact, but if that's not necessary you can use the original and omit the copy. Shown using a loop to demonstrate how to select an arbitrary number of times upto the size of the original array.

var swf=["1.swf","2.swf","3.swf"];
var elementsToChoose = 2;
var copy = swf.slice(0);
var chosen = [];

for (var j = 0; j < elementsToChoose && copy.length; ++j) {
   var i = Math.floor(Math.random()*copy.length);
   chosen.push( copy.splice(i,1) );
}

for (var j = 0, len = chosen.length; j < len; ++j) {
   alert(chosen[j]);
}

Comments

1

I would prefer this way as the bounds are known (you are not getting a random number and comparing it what you already have. It could loop 1 or 1000 times).

var swf = ['1.swf', '2.swf', '3.swf'],
    length = swf.length,
    i = Math.floor(Math.random() * length);
    firstRandom = swf[i];

// I originally used `delete` operator here. It doesn't remove the member, just
// set its value to `undefined`. Using `splice` is the correct way to do it. 
swf.splice(i, 1);
length--;

var j = Math.floor(Math.random() * length),
    secondRandom = swf[j];


alert(firstRandom + ' - ' + secondRandom);

Patrick DW informed me of delete operator just leaving the value as undefined. I did some Googling and came up with this alternate solution.

Be sure to check Tvanfosson's answer or Deceze's answer for cleaner/alternate solutions.

11 Comments

delete doesn't compress the Array. It replaces the value with undefined. So you can end up with "1.swf - undefined".
@alex - Comment out the delete line with an explanation, and replace it with swf.splice(i,1);.
@deceze Well I made an edit, cited the other two solutions and I linked and voted them up. Anything else I can do? :)
@alex - You have list.splice(... It should be swf.splice(.... You don't want the = assignment since .splice() modifies the original array. If you do swf = swf.splice(... then swf will equal the item that was removed.
1.swf - 1.swf or 1.swf - undefined
|
1

This is what I would do to require two numbers to be different (could be better answer out there)

var swf=["1.swf","2.swf","3.swf"];
var i = Math.floor(Math.random()*swf.length);
var j;
do {
    j = Math.floor(Math.random()*swf.length);
} while (j === i); 

alert(swf[i]);
alert(swf[j]);

Edit: should be j===i

2 Comments

The larger the array and the closer you get to choosing all of the elements, the longer this will take. Using the Fisher-Yates shuffle and choosing the first N entries is probably the best, general answer.
Damn, just spot the error that made the code completely useless. Corrected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.