Vice City: Multiplayer

Server Development => Scripting and Server Management => Snippet Showroom => Topic started by: EK.IceFlake on Feb 06, 2015, 05:16 AM

Title: Random number generator - with from and to!
Post by: EK.IceFlake on Feb 06, 2015, 05:16 AM
Hi, guys! After a bit of number-quenching and guesswork, I finally came up with my function that takes a random number from rand() and does some maths to make the from and to function work! It supports negative numbers, too. And also a mixture of negative and positive.
Here is the code:
function Random(from, to) return (rand()*(to+1-from)) / (RAND_MAX+1)+from;Purely tested.
Hope you like it!
Title: Re: Random number generator - with from and to!
Post by: MacTavish on Feb 06, 2015, 02:00 PM
How And Where to use it Please Post An Example
Title: Re: Random number generator - with from and to!
Post by: . on Feb 06, 2015, 03:51 PM
Quote from: Beztone on Feb 06, 2015, 02:00 PMHow And Where to use it Please Post An Example

When you want a random number. Where else would you use it.
Title: Re: Random number generator - with from and to!
Post by: EK.IceFlake on Feb 07, 2015, 04:35 AM
Quote from: Beztone on Feb 06, 2015, 02:00 PMHow And Where to use it Please Post An Example
note this is my idea if you want to use it please give me credit
function onPlayerCommand(player, cmd, arguments)
{
 if(cmd=="snatchkeys")
 {
  if(/*playerisnearanotherplayer(player)*/==null) ClientMessage("[#FFFFFF]You Are Not near AnOther Player!!");
  else if(Random(1,255) != 128) ClientMessage("[#FFFFFF]You Failed To Snatch A Keys!!");
  else
  {
   //...
  }
 }
}
Note: The message grammar is made funny on purpose to mess with you.
Title: Re: Random number generator - with from and to!
Post by: BigcaT_ on Feb 07, 2015, 05:58 AM
GOOD JOB 8)
Title: Re: Random number generator - with from and to!
Post by: EK.IceFlake on Feb 07, 2015, 07:18 AM
Quote from: BigcaT_ on Feb 07, 2015, 05:58 AMGOOD JOB 8)
Thanks.
Title: Re: Random number generator - with from and to!
Post by: Gulk on Mar 22, 2015, 11:16 AM
Thanks for sharing, but does anyone know how I can get a random number from a selection of numbers? Only (12,33,93)
Title: Re: Random number generator - with from and to!
Post by: . on Mar 22, 2015, 11:24 AM
Quote from: Gulk on Mar 22, 2015, 11:16 AMThanks for sharing, but does anyone know how I can get a random number from a selection of numbers? Only (12,33,93)

Put them in an array and then select a random index from that array:
local rnums = [23, 56, 543, 4354, 343, 5897, 564, 425];

print(rnums[Random(0, rnums.len())]);
print(rnums[Random(0, rnums.len())]);
print(rnums[Random(0, rnums.len())]);