0

Can i put a random number function inside an array? If tes, then how!!

19th Apr 2017, 6:49 AM
Biprajit Saha
Biprajit Saha - avatar
5 Answers
+ 8
@michael c: In this case, you don't really use an array, but the object ( in JS all is objects, even arrays ) possibility to add some property to any: var array = []; array["c"] = 46; // equivalent to array.c = 46, but array.length still == zero, and JSON.stringify(array) return '[]', an empty array ^^
19th Apr 2017, 5:21 PM
visph
visph - avatar
+ 6
ahhh so you want to choose a random value from an array? if so then it is very simply using the randVal function from my other answer: var arr = [2, 73, 21, 42, 973, 53, 8, 621] to choose a random value simply generate a random index: randNum = arr[randVal(0, arr.length - 1)]; now randNum is a random value furthermore, if you wish to select a number once only, generate random index, get the value and then splice the index from the array: ind = randVal(0, arr.length - 1); randNum = arr(ind); arr.splice(ind, 1);
19th Apr 2017, 7:18 AM
Burey
Burey - avatar
+ 4
can you please be more specific? do you mean to insert a random value into an array? like this? function randVal (min,max){ /* function to generate randome number in range [min, max] */ return Math.floor(Math.random()*(max-min+1)+min); } var arr = [] arr.push(randVal(10,42)); or do you mean something else?
19th Apr 2017, 7:04 AM
Burey
Burey - avatar
+ 2
var array = []; array["c"] = 46; document.write(array["c"]); this will output value of c! still not clear on your question and I do not believe this is what you were looking for but it does output a number from an array!
19th Apr 2017, 8:32 AM
michael c
michael c - avatar
+ 1
Suppose i have a large concated array and i want display something like C1() + c2() +c3() Snd each of it will display a random output from array
19th Apr 2017, 7:07 AM
Biprajit Saha
Biprajit Saha - avatar