SA-MP Forums Archive
help with randoms - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: help with randoms (/showthread.php?tid=82670)



help with randoms - happyface - 19.06.2009

i want to set up simple stock market, that gradually grow bigger, but can crash at anytime

i am a very begginer and would really appritiate it if someone would just quickly show me how i could do this

i know this code is wrong but it show basically what i am trying to get done, could you please help me with it
pawn Code:
new StockPrice = 100; //starts at $100 a share

forward StockIncreaser();
pawn Code:
OnGameModeInIt()
{
   SetTimer("StockIncreaser",5000,true);
}
pawn Code:
StockIncreaser()
{
   StockPrice = StockPrice+random(50) || Test-random(20);
//every 5 seconds it go up or down randomly with a greater chance to grow bigger, so it slowly grow big over time

//if the last random worse then -19, very uncommon, it trigger stock to crash
   if last random <= -19
   {
     StockPrice = StockPrice-random(200); //huge chance it be worse then -19 again, causing stock to crash to $0 share
//if it not worse then -19, go back to other random
   }

   if(StockPrice <=0)//then if it crash back to $0 a share, restart it all over
   {
    StockPrice = 50;
    killTimer("StockIncreaser");
    SetTimer("StockIncreaser",5000,true);
   }
}



Re: help with randoms - Weirdosport - 19.06.2009

Well the way you've got it set up the stock value will always increase, it won't hover around.

StockPrice = StockPrice+random(50) || Test-random(20);

This is incorrect, not sure what Test is, and what you're trying to achieve.


Re: help with randoms - dice7 - 19.06.2009

pawn Code:
#include <a_samp>

new StockPrice = 100;
new currand;
new StockTimer;


public OnGameModeInit()
{
    StockTimer=SetTimer("StockIncreaser",5000,true);
    return 1;
}
forward StockIncreaser();
public StockIncreaser()
{
    currand = random(200-(-25)+1)+(-25); //creates rand between 200 and -25
    StockPrice = StockPrice+currand;

    if (currand <= -19)
    {
        StockPrice = StockPrice-random(200);
        StockPrice = 50;
        KillTimer(StockTimer);
        SetTimer("StockIncreaser",5000,true);
    }
    return 1;
}
Untested, but it should work


Re: help with randoms - Weirdosport - 19.06.2009

random(200-(-25)+1)+(-25)

What is that!? Don't make the computer do calculations you could do for it.

Assuming you meant:

random(200-(-25)+1) + -25

It'd just be:

random(226) - 25 Which I don't think is what you wanted anyway..


Re: help with randoms - dice7 - 19.06.2009

No, It works perfectly. You can debug it and you'll see. The idea with the rand function is random(max-min+1)+min


Re: help with randoms - happyface - 20.06.2009

i change it a little, and it works great except that it seems to ignore the <= -19

know how to fix?

pawn Code:
#include <a_samp>

new StockPrice = 100;
new currand;
new StockTimer;
forward StockIncreaser();
forward StockChecker();
pawn Code:
public OnGameModeInit()
{
    StockTimer=SetTimer("StockIncreaser",5000,true);
    SetTimer("StockChecker",5000,true);
    return 1;
}
pawn Code:
public StockIncreaser()
{
    if (currand <= -19)
    {
        currand = StockPrice-random(500);
    }
    currand = random(100-(-25)+1)+(-25);
    StockPrice = StockPrice+currand;
    printf("Stock Price: $%d",StockPrice);
    return 1;
}
pawn Code:
public StockChecker()
{
    if(StockPrice <=0)
    {
    KillTimer(StockTimer);
    StockPrice = 50;
    SetTimer("StockIncreaser",5000,true);
    currand = 1;
    }
}