How to check a random number
#1

Ok so there is this thing where a player each gets a number and its a different number its just when i do this

pawn Code:
new check = random(Player[i][Number]);
if(check == Player[i][Number])
{
        // Code here
}
Why does that not work because i want it to pick a random number but only from the numbers that players have

Please help

Thanks
Reply
#2

Please help

Thanks
Reply
#3

Please help

Thanks
Reply
#4

Why does no one help me

Please help

Thanks
Reply
#5

Explain more.
Reply
#6

Quote:
Originally Posted by [MM]RoXoR[FS]
View Post
Explain more.
This does not even pick a number it dont get as far as sending the message

Please help

pawn Code:
new lotto = random(Player[i][RaffleTicket]);
                new Winnings = Player[i][JackPot];
                new string[128];
                if(lotto == Player[i][RaffleTicket])
                {
                    format(string, sizeof(string), "Congratulations %s has won the lottery of %d", GetNameNoUnderScore(i), Winnings);
                    SendClientMessageToAll(YELLOW, string);
                    format(string, sizeof(string), "Congratulations you have won the lottery of %d", Winnings);
                    SendClientMessage(i, YELLOW, string);
                    SendClientMessageToAll(YELLOW, "Remember to buy a another raffle ticket if you want to take part in the next lottery");
                    Player[i][RaffleTicket] = 0;
                    Player[i][Money] += Winnings;
                }
Please help

Thanks
Reply
#7

lets assume i got my number set to 100:
pawn Code:
Player[i][Number]=100;
..so check will be 100 aswell:
pawn Code:
new check = Player[i][Number];
..taking the random out of (this variable set to) 100, is like
pawn Code:
new check = random(100);
...which returns a number ranging up from 0 to the maximim (100) minus 1.
0 upto 99 ARE 100 numbers, basically the "hit chance" is 1% when the playervariable is set to 100 and a random from 0-99 is taken. to achieve that, you can do this:
pawn Code:
new check = random(Player[i][Number]);
if(check == 0)
or, much simpler, in 1 line (checking for 0 with a max 100 = 1% chance) as before:
pawn Code:
if(random(Player[i][Number] == 0)
..also have a look @ https://sampwiki.blast.hk/wiki/Random
Reply
#8

Quote:
Originally Posted by Babul
View Post
lets assume i got my number set to 100:
pawn Code:
Player[i][Number]=100;
..so check will be 100 aswell:
pawn Code:
new check = Player[i][Number];
..taking the random out of (this variable set to) 100, is like
pawn Code:
new check = random(100);
...which returns a number ranging up from 0 to the maximim (100) minus 1.
0 upto 99 ARE 100 numbers, basically the "hit chance" is 1% when the playervariable is set to 100 and a random from 0-99 is taken. to achieve that, you can do this:
pawn Code:
new check = random(Player[i][Number]);
if(check == 0)
or, much simpler, in 1 line (checking for 0 with a max 100 = 1% chance) as before:
pawn Code:
if(random(Player[i][Number] == 0)
..also have a look @ https://sampwiki.blast.hk/wiki/Random
Very good explaining but im still kind of confused its a lottery and i need ti so it chooses a raffle ticket that someone has already got.

It cant choose a number that a player has not got.

Please help

Thanks
Reply
#9

Please help

Thanks
Reply
#10

ok, if i didnt get you wrong, you got a simple lottery system, where any player can chose a /lotto number, ranging up from 1 to 100 - then at a time/command, the server shall see which numbers are being chosen by players, and draw one of them?
if so, heres some thoughts about solving your little problem:
-each player choses a number, and its supposed to be chosen once only. since this is the case,
-an array holding each number - chosen by which players aswell later - is required:
pawn Code:
new NumberTaken[100];//if you /lotto 13, then NumberTaken[13]=1 so no player can bet on 13 anymore.
...now no number can be chosen twice, the array will get filled up with 1's when players play different numbers.
-the sizeof the numbers array is 100, a linear search through NumberTaken[] to collect all drawn numbers is a start..
-after all "only bet on" numbers are known, the new temoprary array AllNumbersTaken[] <= NumberTaken[100] after counting/collecting the players' bets.
-what when the 13 wins? the server knows THAT its the 13, and that there is 1 winner only indeed, due to the nature of "1 player per number" - so instead of looping through all players and checking for the /lotto number, the winner can be estimated the same way as the lotto number 13 itself. use an array, filled with pointers! an array like
pawn Code:
new NumberTakenByID[MAX_PLAYERS];
...will serve as a pointer table - each cell contains a playerid, yours should be stored in cell [13], right?
when you type /lotto 13, and the NumberTaken[13]==0, thhe following line will set the same cell (13) to your playerid, to let the script get pointed to your playerid by accessing the winner cell 13:
pawn Code:
NumberTaken[13]=1;//old one. your bet got accepted...
NumberTakenByID[13]=playerid (yours);//the 13 should be a sscanf-ed variable indeed...
so a loop to get the winner, could look like:
pawn Code:
new AllNumbersTaken[100];
new Found;//0 numbers are known to be potential winners, yet.
for(new ANT=1;ANT<100;ANT++)//all numbers from 1 to 100
{
if(NumberTaken[ANT]==1)//gets checked if theyre taken by any playerid
{
AllNumbersTaken[Found]=ANT;//start at cell 0 of the temp array, and
Found++;//prepare to write into the next cell.
}
}
new NumberWins=random(sizeof(AllNumbersTaken));//pick a random lotto number from the "played only" table
now, to get the playerid(yours) of the winner, simply read it out of the NumberTakenByID array which holds the playerid in each lotto-number cell, thats where the pointer steps in:
pawn Code:
new JackpotID=NumberTakenByID[NumberWins];
hm.. i hope i didnt forget something vital.. ah, the new declarations are missing, but youll add them when converting your variable names. i like the idea of a lottery where always one player wins, but makes the jackpot not dynamic. think ybout a way to increase the motivation of players to play lotto, like a percentual amount of the server economy added to the jackpot, or donations being added to it aswell...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)