Jony Kale
Jony Kale

Reputation: 979

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

Okay I have kind of a spin system, you spin and it generates a random number.

If the number is less than 100, you will win.

But how can I make it so, the lower the number his, the higher coins you will get

Currently i 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>';
        }
    }

$prize is the prize. Basically now I am multi piling it by 250 and dividing by 2. so if I get the number '1'. i will get an awful prize.

How do I do that?

Upvotes: 4

Views: 665

Answers (6)

Jony Kale
Jony Kale

Reputation: 979

Solved it with a little of thinking and calculation.

1000 - 250 / 100 = 7.5

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

Results:

x(1) = 1000

x(100) = 250

Upvotes: 2

David Chan
David Chan

Reputation: 7505

you are trying to define a math function f(x) where f(1) = 250 and f(99) = 1000;

there are lots of possible shapes which will.

i suggest you graph 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 here are you have a function which generates a values for the series between 1-100.

  1. invert the input (101-number) so that smaller inputs produce bigger results.
  2. map the output to your scale... which is between (0 to 750) by multipling 750 by a ratio.
  3. translate your scaled number to 250 which is your minimum

Upvotes: 0

ElmoVanKielmo
ElmoVanKielmo

Reputation: 11290

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 as author desires.

Upvotes: 0

MythThrazz
MythThrazz

Reputation: 1647

Maybe you'll try this way:

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>';
    }
}

edit.

I've found a function which gives the expected results for the given numbers. Hope it's ok. Data source: Wolphram Alpha

Upvotes: 0

Amar Banerjee
Amar Banerjee

Reputation: 5012

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;
}

Upvotes: 0

Marc
Marc

Reputation: 426

Here is one way. Just takes the opposite of the number using a maximum.

function getNumPrizes($number)
{
    $maxPrizes = 100;

    // using max so we dont get less than 0
    $prizesWon = max(($maxPrizes - $number) + 1, 0);

    return $prizesWon;
}

This way you'll end up getting 100 coins for a 1, 99 for a 2, etc.

You could then run $prizesWon through another function to scale it how you wish.

Upvotes: 0

Related Questions