1

Needing to write a function that returns a number between 0 and 6 (inclusive)with the following probabilities:

0: 10%, 1: 26%, 2: 33%, 3: 18%, 4: 9%, 5: 3%, 6: 1%

While this is likely best done with objects, I'm not that far along... so think I need to iterate over 2 arrays (say var values=[ 0,1,2,3,4,5,6] and var weights=[10, 26, etc]

My ridiculous question: in arrays: how do I return values[i] a [weighted] number of times .... all my syntax efforts return the two values multiplied (basically values[i] * weighted[i]).

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
PathGeek
  • 21
  • 2
  • Do I understand you correctly that you want a random number between 0 and 6 weighted as you specified? Please share your attempts with us. – TimoStaudinger Nov 19 '16 at 23:40
  • correct. first I tried doing this without iteration as follows (but didn't get it right). In a test set, I tried the following, but it multiplies each [i] with the value, instead of returning that "n" of [i] var a = [1,2,3]; var b= [3,2,1]; var myArr = []; for (var i = 0; i < a.length; i++) { myArr.push(a[i] * b[i]); } console.log(myArr); (output [3, 4, 3]) what I'm hoping to output is: [1,1,1,2,2, 3] – PathGeek Nov 20 '16 at 03:49

0 Answers0