Jump to content
Sign in to follow this  
Cloughy

Geneate a random number between two values

Recommended Posts

I was wondering if anyone would know how to generate a random number between two vlaues?

I know the random function in the game only generates from 0 to the number specified.

Cheers

GC

Share this post


Link to post
Share on other sites

Use your imagination a bit...

If you want a random number between 13 and 44 fe:

the difference is 31, so you do random 31, and add 13 to that

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_value = (random 31) + 13

this doesnt return only integers so you might wanna use the floor() function

BIKI

Share this post


Link to post
Share on other sites

why use floor? i think its better to use round command

Share this post


Link to post
Share on other sites

I think the floor function is better with random function because you want each integer to have the same likehood. round can be unpredictable because it can either floor or ceil.

I might be wrong but sounds logical to me

Share this post


Link to post
Share on other sites

from wat i understan from the wiki floor Returns the next lowest integer.. so what if he's random vaule is 43.99. with floor wount that become 43? with round that will become 44.. if i got it correct

Share this post


Link to post
Share on other sites

Its better to use floor when you select something random from an array, with round the first and the last option would only have 0.5 chance while all the other 1. smile_o.gif

Share this post


Link to post
Share on other sites

hmm! lets take rundll.exe example. where you need a random number between 31 and 44..

with floor you will never get 44 unless the random vaule is exactly 44 ? right?

Share this post


Link to post
Share on other sites

Although couldn't you also use something like :

round ((random x + 1) - 0.5)

to achieve the same result?

Share this post


Link to post
Share on other sites
hmm! lets take rundll.exe example. where you need a random number between 31 and 44..

with floor you will never get 44 unless the random vaule is exactly 44 ? right?

Actually, random never includes the given number, in this case it would give a number between 13 and 43.9999

Share this post


Link to post
Share on other sites

so then you would need to add 1 to your upper value:

for the 13 to 44 example

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_value = floor(random (31+1)) + 13

Share this post


Link to post
Share on other sites

Shuko yea your right.. you will never get the number.. i did a test on it..

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">

_a = 0;

while {(true)} do {

_vaule=(random 31) + 13;

_a=_vaule;

_r = round _a;

_f = floor _a;

hint format["round = %1\nfloor = %2\nrandom = %3", _r, _f, _value];

sleep 0.5;

};

if you test this.. you see that floor never becoms 44.

so i see no point in using floor if you need the value 44..

i still think round is the proper way.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×