Random number generator - with from and to!

Started by EK.IceFlake, Feb 06, 2015, 05:16 AM

Previous topic - Next topic

EK.IceFlake

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!

MacTavish

How And Where to use it Please Post An Example

Grand Hunting Project
Join #SLC, #KAKAN, #Doom, #GHP @LUnet

Retired VC:MP Player/Scripter :P

.

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.
.

EK.IceFlake

#3
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.


EK.IceFlake


Gulk

Thanks for sharing, but does anyone know how I can get a random number from a selection of numbers? Only (12,33,93)

.

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())]);
.