what it does is randomly picks a car from one of two lists chosen by the user.
the only thing is that the choice does not get reflected in the select field.
how to make it happen?
_____________________________________________________________
EDIT for clarity
Problem
once you click your choice of the country, the code should display in the select field a randomised brand, which it doesnot. it shows always either chevrolet of ford - the first cars from the list.
another indication that not all is well is that the alert appears twice each time, while it should only once.
yes, that's what i think the code is supposed to do.
it chooses the right select field by its id
Code:
var select= document.getElementById('s1');
it checks the number of items
Code:
var items = select.getElementsByTagName('option');
picks a random number of option
Code:
var index= Math.floor(Math.random() * items.length);
and makes the option by this number selected
Code:
select.selectedIndex = index;
in separation from the rest of the code, it works as expected.
but in combination, there is always either ford or chevrolet, i.e. the first options selected. considering that the alerted random numbers vary, this part of the code gives no effect.
The following code worked for me when I tested it. Refreshing the page enough times eventually gets all the values to display and although there was a bias toward 1 & 2 for a while it evened out as I refreshed more times.
The following code worked for me when I tested it. Refreshing the page enough times eventually gets all the values to display and although there was a bias toward 1 & 2 for a while it evened out as I refreshed more times.
I do not detect any bias. It worked perfectly on my tests.
Be aware that random implies that the same number may be generated 2,3 or possibly even more times in succession, especially with a small number (6) of values.
Another approach would be to shuffle the numbers 1-6 into a random order, and then pick the next one in sequence, thus avoiding consecutive
appearances of the same number.
Code:
<script type = "text/javascript">
Array.prototype.shuffle = function() {
var s = [];
while (this.length) s.push(this.splice(Math.random() * this.length, 1));
while (s.length) this.push(s.pop());
return this;
}
var count = 0;
var arr = [1, 2, 3, 4, 5, 6];
var sarr = arr.shuffle);
document.write(sarr); // for testing
function shownext() {
alert (sarr[count]); // for testing
count ++;
if (count >5) {count = 0}; // start the sequence again
select.selectedIndex = count;
}
</script>
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
I have the idea in my mind that this sort function is biased. I may be wrong. Felgall may comment.
Yes - I ran tens of millions of tests of that sort code and found a definite bias toward values staying in the same place. You need to use a value of about 0.7 something to get closer to an unbiased result - 0.7 was still biased toward staying in place and 0.8 was biased the other way and I decided to not bother trying to run the trillions of tests that would be necessary to properly narrow it down further. Presumably there would be a way to calculate the value that would produce an unbiased result with that formula but it is easier to just replace it with a shuffle instead.
With the code I posted above that initially displayed a bias to the first two numbers (each of which appeared several times before a 5 appeared) I just kept refreshing the page and before long was getting a more even result - you would expect that you might sometimes get a bias like that with a properly random function and only when you check a large number of times would you expect the appearances of each to start to even out. Since there is nothing in that code that I know of that could introduce a bias to the results I can't see any point in setting up an automated test to record the outcomes of millions of tests the way I did to check whether the sort random function. I hadn't initially realised that the random sort was biased until someone suggested to me that a random compare of pairs of entries did not necessarily lead to random results being returned from the sort - it was then that I set up an automated test that sorted 10 numbers a million times and recorded how many times each number ended up in each position - which I then ran about a dozen times just to check that the bias that one test showed wasn't a fluke. I then started increasing the value from 0.5 to 0.6 and then 0.7 and 0.8 to see that the bias gradually reduced and then reversed. By that point I'd manually run the test about 50 times with each test running a million sorts.