Random & Round
#1

I have been attempting to create a selling feature to sell off unwanted vehicles and am using the random() function. its not quite what i would like as i use alot of php but if i cant do anything about it i will settle for it.

basically i want to be able to specify a range that a random number can be

ie any number between 20 and 40

the function appears to only work from 0 - the number you specify

I would also like to be able to round this random number up or down to the nearest 10 or 100 (not sure which yet) but i have been unable to find any documentation saying there is a round() function.

can anyone help?
Reply
#2

You can specify a range using something like that:

pawn Код:
random(maximum-minimum)+minimum
Reply
#3

still would like to see something about round()

anyone?

and im not sure i understand your maximum-minimum within the brackets, surely that could create a negative random number or a maximum number that isnt as high as i want.
Reply
#4

pawn Код:
new Random = random(20) + 20; //Will never return less than 20 or over 40 :).
Reply
#5

what if i want my minimum to be 200 and my maximum to be 5000?

its only any good if i want double my minimum to be my maximum.

The script is written to sell vehicles. this is what i have:

Код:
if(strcmp(cmdtext, "/sell", true) == 0){
if(IsPlayerInAnyVehicle(playerid) && GetPlayerVehicleSeat(playerid) != 0){
 SendClientMessage(playerid, 0xFFFFFFFF, "You are not driving this vehicle!");
}
if(!IsPlayerInAnyVehicle(playerid)){
 SendClientMessage(playerid, 0xFFFFFFFF, "Your not in a vehicle!");
}
if(IsPlayerInAnyVehicle(playerid) && GetPlayerVehicleSeat(playerid) == 0){

     new carvalue;
     new carvaluedamaged1;
     new carvaluedamaged2;
     new currentveh;
	   	 new Float:health;
	   	 
     currentveh = GetPlayerVehicleID(playerid);

     GetVehicleHealth(currentveh, health);
  // return SendClientMessage(playerid,COLOR_RED,"Doesn't need repairing!");
	   	carvaluedamaged1 = random(2500);
    carvaluedamaged2 = random(900);
    
	   	 if(health == 1000){
     carvalue = random(5000);
     }
     else if (health >999){
	   	 carvalue = carvaluedamaged1;
	   	 }
	   	 else if (health >550){
	   	 carvalue = carvaluedamaged2;
	   	 }
if(!IsPlayerInInvalidSellVehicle(playerid)){
     DestroyVehicle(currentveh);
     format(string, sizeof(string), "You get %i for your vehicle!", carvalue);
     SendClientMessage(playerid, 0xFFFFFFFF, string);
     GivePlayerMoney(playerid, carvalue);
}
else{
  SendClientMessage(playerid, 0xFFFFFFFF, "This vehicle can't be sold!");
}

}
return 1;
}
im not even sure if the health bit is working properly at the moment but i cant see why it wouldnt.

This script does work.
I created this function: IsPlayerInInvalidSellVehicle(playerid)
using the array feature someone else used for their IsPlayerInInvalidNosVehicle() funtion and just changed the name and the vehicle ids

ok well if the random feature isnt flexible enough is there ANY sort of round feature this is the main reason i created the post.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)