/    Sign up×
Community /Pin to ProfileBookmark

JavaScript and Array’s

If i wanted to generate an array that contains 16 numbers (from 0 to 15 inclusive) but i wanted the 16 numbers to be arranged in the array randomly.

e.g. 0,3,9,1,2,15,12 etc

how would i go about doing this. i have been told that:

[CODE]
var randomNumber = 0;
for (var k = 0; k <= 16; k++)
{
randomNumber = Math.floor(1 + Math.random() * 15);
numbers[k] = numbers[randomNumber];
}

// numbers is an array containing 16 positions (they must be WHOLE integers)
[/CODE]

the code above would work, but any fool can see the faults in that. my tutor said that this would work because the possibility of the same number occuring twice would be slim, well i can assure you on every trial i have done i have had 4 or more of the same number appear.

Please help, would i have to use two arrays numbers[] and a temp array, and then check every single number as it is sent through to see if it has been used of not?

please help

Cheers

?

dkeegh

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@BigMoosieMay 02.2005 — Array.prototype.shuffle=function() {
for(var i=0; i&lt;this.length; i++) {
var num1 = Math.floor(Math.random()*this.length);
var num2 = Math.floor(Math.random()*this.length);
var tempNum = this[num1];
this[num1] = this[num2];
this[num2] = tempNum;
}
return;
}

var moose=new Array();
for (var i=0; i&lt;16; i++) moose[i]=i;
moose.shuffle()
alert(moose);
Copy linkTweet thisAlerts:
@7studMay 02.2005 — my tutor said that this would work because the possibility of the same number occuring twice would be slim,[/QUOTE]
Your tutor certainly doesn't understand probability. The probability that you get at least one number that is the same is nearly 100%.

Please help, would i have to use two arrays numbers[] and a temp array, and then check every single number as it is sent through to see if it has been used of not?[/QUOTE]As a first pass, that would be how you would do it. But, there are various tricks you can employ to make it more efficient.

Big Moosie,

Your function calls Math.random() 30 times. You should be able to do it with only 14 calls to Math.random().
Copy linkTweet thisAlerts:
@tilspam_ofir_dkMay 02.2005 — This function will return a array holding n integers (0 to n) in random order. It runs in linear time

[CODE]function randomIntegers(n) {
var tmpArr = new Array();
// Create array with numbers 0,1,2,..,n
for (var i=0; i<n; i++)
tmpArr[i] = i;
var retArr = new Array();
// Randomize numbers in array
for (var i=0; i<n-1; i++) {
var ranIndex = Math.floor(Math.random()*tmpArr.length);
retArr[i] = tmpArr[ranIndex];
tmpArr[ranIndex] = tmpArr[tmpArr.length-1];
tmpArr.pop();
}
retArr[n-1] = tmpArr[0];
return retArr;
}[/CODE]


To get an array holding 16 (0 to 15) integers in random order call 'randomIntegers(16)'

7Stud> My solution calls the Math methods n-1 times. How would you write it so it only uses n-2 ?
Copy linkTweet thisAlerts:
@7studMay 02.2005 — 7Stud> My solution calls the Math methods n-1 times. How would you write it so it only uses n-2 ?[/QUOTE]

My mistake. BigMoosie calls Math.random() 32 times because there are 16 numbers, not 15. ?
Copy linkTweet thisAlerts:
@BigMoosieMay 07.2005 — ...
×

Success!

Help @dkeegh spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...