1

I am trying to generate a random number and have a probability of x% to get closer of the max value.

Problem is that I have no idea how to do this, unless I make some steps?

Let's say I'll generate a random number between 1 and 3000.

$number = mt_rand(1, 3000)

This will return 2700 for example. I would like it to be closer to 1 than 3000.

How should I implement a function that will only have a 10% chance to get near 3000?

Iulian
  • 25
  • 5
  • Can you explain "near 3000"? is it "between 2500-3000" or "2900-3000"? – Dekel Jan 02 '17 at 21:26
  • More likely to get the smallest amount possible (let`s say a range of 1-100) but still have that 10% chance to get near 3000 (with a range of 1000-3000). – Iulian Jan 02 '17 at 21:28
  • The question is how you divide the ranges... what exactly is "near 3000" (you still didn't answer this) – Dekel Jan 02 '17 at 21:32
  • Ok. So I should set the range to 1 - 100 most of the time. But still have a 10% chance to make the range 1 - 3000. – Iulian Jan 02 '17 at 21:38
  • Define "near 3000"? Is this curve symmetrical, like a bell curve? It's hard to devise a specific solution to a vague problem statement. You should try to define your terms more clearly. – clearlight Jan 03 '17 at 00:19
  • What about generating a random number between 0 and 1, and using `log()` or `sin()` or `cos()` tweaking the curve formulaically to get the proportions you want and symmetry or lack thereof, and use the resulting value of the curve deflection along the appropriate axis as a percentage of your target range (in this case 3000)? You could control the 'chances' of of hitting numbers at the extrema that way. – clearlight Jan 03 '17 at 00:27
  • Or you could try to control the percentages by making smaller random numbers and using a switch statement with each case defining a different range, then choose a new random number within each range, or some less analog scheme like that. – clearlight Jan 03 '17 at 00:29
  • Or you could pre-populate a larger array, let's say 300,000 elements, with numbers with each number repeated based on the % of time it should be hit, then choose a number between 0 and 300,000 and pluck the value out of that entry in the array... – clearlight Jan 03 '17 at 00:30

1 Answers1

0

Based on the comments on the question you can use the mt_rand function twice. First - to check if it should be the 90% (where the value is random of 1-100), and if not - it is the other 10%, where the value is random of 1-3000:

$number = mt_rand(0, 9) == 0 ? mt_rand(1, 3000) : mt_rand(1, 100);
Dekel
  • 60,707
  • 10
  • 101
  • 129