-9

From user input of either char a or char b as an answer. I want to return a value of true or false from user input. Then count the number of selections between two possibilities per individual user input. Then force it to randomly return false 3% of the time. The purpose is to alter the expected outcome with a reward.

How do I go about doing this? I am new to this concept.

Javacodeman113
  • 407
  • 1
  • 4
  • 11
  • Use a random number between 1-100 and return false for (1,2,3) and true for the rest. – ArcticLord Oct 26 '16 at 08:31
  • First problem: work out how to generate a random number. If you could generate a random number between 0 and 99 inclusive, do you think you could convert that to returning false 3% of the time and true otherwise? – Jon Skeet Oct 26 '16 at 08:31
  • of course depending how you get that random number into the range 0 - 99 might make it biased..... as in don't rely on the low order bits of PRNG – Mitch Wheat Oct 26 '16 at 08:33
  • Reworded my question for clarity. I am not using numbers initially only a use selection of two possibilities. – Javacodeman113 Oct 26 '16 at 09:08
  • Sorry the question was not already answered but I found a solution that will work for a design pattern. – Javacodeman113 Oct 26 '16 at 09:22
  • `if (Math.random() < 0.03) return false;` – Boann Oct 26 '16 at 18:33

2 Answers2

1

Check out Math.random(), which gives you a random number [0,1). You want false 3/100 times, so if you multiply your random number by 100, your range will become [0, 100). Then just check if the random number is less than 3, if so, return false.

khelwood
  • 55,782
  • 14
  • 81
  • 108
Mindie Tea
  • 26
  • 3
1

here how to generate random num in range. You should use range [1-100]. If it gives you 1,2 or 3 return false, else true

Community
  • 1
  • 1
Gio
  • 33
  • 6