-1

hallo i am really new at this google javacript, and i want to learn this languange. And now im trying to build flip-coin alike program, so the algorythm is, i have 2 variable, just say head and tail variable

then it shows result as a random between that head or tail. may u provide me some syntax for it? very big apreciate for it. it just an example, i will learning and expanding it

and, will u recommend me the e-book for learning this languange? for a newbie like me thanks

SADmiral
  • 41
  • 4
  • 1
    Possible duplicate of [JS generate random boolean](https://stackoverflow.com/questions/36756331/js-generate-random-boolean) Also, welcome to Stack Overflow! It's good practice to show what you've tried in a question. – Félix Paradis Apr 14 '19 at 16:47

2 Answers2

0

The arr variable is [head,tail]

function randomChoice(arr) {
    return arr[Math.floor(arr.length * Math.random())];
    //returns the value of head or tails
}
atakanyenel
  • 1,367
  • 1
  • 15
  • 20
0

The Math.random() function returns a floating-point, pseudo-random number in the range 0–1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range. The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.

Shunker
  • 26
  • 2