Vice City: Multiplayer

Server Development => Scripting and Server Management => Topic started by: umar4911 on Dec 25, 2017, 11:28 AM

Title: Random string and random number
Post by: umar4911 on Dec 25, 2017, 11:28 AM
How to make a function that makes a random string containing 4 to 9 characters.
another function to select a random number from 500 to 3000.
Title: Re: Random string and random number
Post by: ! on Dec 25, 2017, 11:43 AM
http://forum.vc-mp.org/?topic=2342.msg17237#msg17237
Quote from: . on Mar 02, 2016, 03:13 AMSelf explanatory. True random floating point numbers with actual fractional part that is also random. Not integers converted to floating points without a fractional part.

const INT32_MAX = 0x7FFFFFFF;
const INT32_MIN = 0x80000000;
FLOAT32_MAX <- INT32_MAX.tofloat();
FLOAT32_MIN <- INT32_MIN.tofloat();

srand((GetTickCount() % time()) / 3);

function RandomFloat()
{
    return (FLOAT32_MAX - FLOAT32_MIN) *
            ((rand() % 0x7FFF).tofloat() / (RAND_MAX).tofloat()) + FLOAT32_MIN;
}

function RandomFloatUpto(n)
{
    n = n.tofloat();
    return (rand() % 0x7FFF).tofloat() / (RAND_MAX / n).tofloat();
}

function RandomFloatBetween(m, n)
{
    m = m.tofloat(), n = n.tofloat();
    return (n - m) * ((rand() % 0x7FFF).tofloat() / (RAND_MAX).tofloat()) + m;
}

Example:

print( RandomFloat() );
print( RandomFloatUpto(7.4) );
print( RandomFloatBetween(3.2, 5.9) );



http://forum.vc-mp.org/?topic=246.msg1334#msg1334
Quote from: EK.IceFlake on Feb 06, 2015, 05:16 AMHi, 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!



http://forum.vc-mp.org/?topic=246.msg2638#msg2638
Quote from: . 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())]);
Title: Re: Random string and random number
Post by: umar4911 on Dec 25, 2017, 11:49 AM
Quote from: ! on Dec 25, 2017, 11:43 AMhttp://forum.vc-mp.org/?topic=2342.msg17237#msg17237
Quote from: . on Mar 02, 2016, 03:13 AMSelf explanatory. True random floating point numbers with actual fractional part that is also random. Not integers converted to floating points without a fractional part.

const INT32_MAX = 0x7FFFFFFF;
const INT32_MIN = 0x80000000;
FLOAT32_MAX <- INT32_MAX.tofloat();
FLOAT32_MIN <- INT32_MIN.tofloat();

srand((GetTickCount() % time()) / 3);

function RandomFloat()
{
    return (FLOAT32_MAX - FLOAT32_MIN) *
            ((rand() % 0x7FFF).tofloat() / (RAND_MAX).tofloat()) + FLOAT32_MIN;
}

function RandomFloatUpto(n)
{
    n = n.tofloat();
    return (rand() % 0x7FFF).tofloat() / (RAND_MAX / n).tofloat();
}

function RandomFloatBetween(m, n)
{
    m = m.tofloat(), n = n.tofloat();
    return (n - m) * ((rand() % 0x7FFF).tofloat() / (RAND_MAX).tofloat()) + m;
}

Example:

print( RandomFloat() );
print( RandomFloatUpto(7.4) );
print( RandomFloatBetween(3.2, 5.9) );



http://forum.vc-mp.org/?topic=246.msg1334#msg1334
Quote from: EK.IceFlake on Feb 06, 2015, 05:16 AMHi, 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!



http://forum.vc-mp.org/?topic=246.msg2638#msg2638
Quote from: . 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())]);
And string?
Edit: found, need to test
Title: Re: Random string and random number
Post by: . on Dec 25, 2017, 01:59 PM
http://forum.vc-mp.org/?topic=1382.0
Title: Re: Random string and random number
Post by: umar4911 on Dec 25, 2017, 03:24 PM
Mission Accomplished. Solved