PHP spin system. The lower the number, the more rewards you get

Ok, I have some kind of spin system, you spin and generate a random number.

If the number is less than 100, you win.

But how can I do this, the lower the number, the higher the coins you get

I currently have this:

public function getPrize($number) { $prize = $number * 250 / 2; if ($number < 100) { return '<span style="color: green;">You have won lucky <b>'.$prize.'</b> coins!</span>'; } else { return '<span style="color: red;">Sorry but, bad luck. You have won nothing! number: '.$number.'</span>'; } } 
The prize is

$ prize. Basically, now I fasten it a lot by 250 and dividing by 2. so if I get the number "1". I will get a terrible prize.

How to do it?

+4
source share
6 answers

I decided to think and calculate it a little.

1000 - 250/100 = 7.5

 $prize = 250 + (750 - ($number * 7.5)); 

Results:

x (1) = 1000

x (100) = 250

+2
source

Final version:

 if($number < 100){ $prize = round((99.00 - floatval($number)) * 7.653) + 250; else{ $prize = 0; } 

This gives 250 for $number = 99 and 1000 for $number = 1 at the request of the author.

0
source

Here is one way. It is simply taken against the number, using the maximum.

 function getNumPrizes($number) { $maxPrizes = 100; // using max so we dont get less than 0 $prizesWon = max(($maxPrizes - $number) + 1, 0); return $prizesWon; } 

Thus, you will get 100 coins for 1, 99 for 2, etc.

Then you can run $ prizesWon through another function to scale it as you want.

0
source

Here is another solution.

  function getNumPrizes($number) { $maxPrizes = 100; $multiplier = // number you want to multiply with the result. It may be 125 or something else // using max so we dont get less than 0 $prizesWon = max(($maxPrizes - $number) + 1, 0)*$multiplier; return $prizesWon; } 
0
source

You might try as follows:

 public function getPrize($number) { $quad = $number*$number; if ($number<100 && $number>0) { $prize = 0.0525665*$quad-12.885*$number + 1012.83; //http://www.wolframalpha.com/input/?i=quadratic+fit+%281%2C1000%29%2C%2850%2C500%29%2C%28100%2C250%29 return '<span style="color: green;">You have won lucky <b>'.$prize.'</b> coins!</span>'; } else { return '<span style="color: red;">Sorry but, bad luck. You have won nothing! number: '.$number.'</span>'; } } 

to change.

I found a function that gives the expected results for given numbers. I hope everything is in order. Data Source: Tungsten Alpha

0
source

you are trying to determine the mathematical function f (x), where f (1) = 250 and f (99) = 1000;

There are many possible forms that will be.

I suggest that you calculate the results of your functions to help you decide what is best for you.

Here are some examples.

 // linear growth // this produces a straight line function prize($number) return (101 - number) * 75 + 250; // log growth // this produces a log curve where you have diminishing returns function prize($number) return (log(101 - number) -1 ) * 750 + 250; // exp growth // this returns exponential returns function prize($number) return (((101-number)^2)/10000) * 750 +250; 

The basic operations are here: you have a function that generates values ​​for a series between 1-100.

  • inverts the input (101 number), so smaller inputs give greater results.
  • display the output on your scale ... which is between (0 to 750) by multiplying 750 by a factor.
  • translate the scaled number to 250, which is the minimum
0
source

Source: https://habr.com/ru/post/1485430/


All Articles