Jump to content
Sign in to follow this  
tophe

Doing a perfect random number.

Recommended Posts

I'm having a bit of headache over a random number I want.

I need an integer between 1 and 18, so I did this:

_number = floor(random 17) ;
_number = _number + 1;

Which I guess could be written like this right?

_number = floor(random 17) + 1;

The thing is that floor is supposed to give the lowest integer in relation to my random number. So 1.31 gives 1 and 5.92 gives 5 and so on.

The problem is that I guess 17 could only occur if the random number would hit exactly 17, since 16.9 would return 16.

This means that the chance of getting 17 (which will turn into 18 in my script) is much smaller than any other number.

If I use ceil instead I get the same problem but the other way around.

Also, round, will give me similar problems since, for example, 0 would only be returned if the random number is between 0 and 0.49.

While 1 would be returned if the random number is between 0.5 and 1.49.

I could of course do:

_number = floor(random 18) + 1;
if (_number == 19) then {_number = 18};

Which would give the number 18 a tiny larger chance (about 0.01 more right?)

Or:

_number = ceil(random 18);
if (_number == 0) then {_number = 1};

Which would give the number a tiny larger chance.

I don't know... help me out guys, what do you think?

I'm making a super awesome mission here...

Edited by Tophe

Share this post


Link to post
Share on other sites

What about increasing the count, and having the highest and lowest numbers causing a re-randomization? Bit of a cheap hack, but... meh

Share this post


Link to post
Share on other sites
What about increasing the count, and having the highest and lowest numbers causing a re-randomization? Bit of a cheap hack, but... meh

Good idea... kind of like this?

_number = 0;
while {(_number < 1) || (_number > 18)} do 
{
_number = floor(random 19);
};

Share this post


Link to post
Share on other sites

Um... I'm half-drunk and haven't slept in a long, long time, so... yeah, sure, why not.

Share this post


Link to post
Share on other sites

Having just run a test on six numbers only I found all that would be needed is

 _number = floor(random 18) + 1;

18 would get picked just as often as any other number.

Share this post


Link to post
Share on other sites

Depends on what do you need the random number for?

If you want to use it to get random element from an array, then all you need is

_array = ["a","b","c"];
_r = random 2;
_item = _array select _r;

Since the Random returns number between 0 (included) and 2 (not included), the smallest returned number can be 0 ("a"), and the highest returned number can be 1.9999999 ("c").

And if you need to get whole number, the best is probably _r = floor(random 5)+1; as someone already posted.

edit: sorry, i didn't understood what do you need exactly (same chance to get any of the whole numbers). So just ignore me, please :)

Edited by 5133p39

Share this post


Link to post
Share on other sites
Depends on what do you need the random number for?

If you want to use it to get random element from an array, then all you need is

_array = ["a","b","c"];
_r = random 2;
_item = _array select _r;

Since the Random returns number between 0 (included) and 2 (not included), the smallest returned number can be 0 ("a"), and the highest returned number can be 1.9999999 ("c").

And if you need to get whole number, the best is probably _r = floor(random 5)+1; as someone already posted.

Thank you, I'll look into that array idea. Never thought of that.

The other solution gives me one small problem....

If I need a number between 1 and 18 and do:

_number = floor(random 18) + 1;

there is a small chance the randomizer will hit 18.00 spot on, which would return the number 19.

I need the number to place a logic at a building.

They are marked with markers called house_1, house_2 etc...

_number = floor(random 18) + 1;
myLogic setPos (getMarkerPos format["house_%1", _number]);

So I might try putting the marker names in an array instead.

I'll end up with a bit more text though. I thought this looked a bit more tidy.

Edited by Tophe

Share this post


Link to post
Share on other sites

I've never had it hit 18 yet and I don't think it will but you could use 17.9999999

Share this post


Link to post
Share on other sites
If I need a number between 1 and 18 and do:

_number = floor(random 18) + 1;

there is a small chance the randomizer will hit 18.00 spot on, which would return the number 19.

The randomizer will NEVER hit 18.00, Random 18 does NOT include the 18.

Wiki says "Random real (floating point) value from 0 (inclusive) to x (not inclusive)."

Edited by 5133p39

Share this post


Link to post
Share on other sites
The randomizer will NEVER hit 18.00, Random 18 does NOT include the 18.

Wiki says "Random real (floating point) value from 0 (inclusive) to x (not inclusive)."

Look at that! It's been ages since I read about random in the wiki... that should have saved me alot of thinking. Thanks man.

So to get an integer between 1 and 18, with equal chance for all numbers, I'll go with:

_number = floor(random 17) + 1;

Thanks everyone for your time and help.

Share this post


Link to post
Share on other sites
Wouldn't it have to be floor (random 18) + 1?

0-17 + 1?

doh!!

Thanks man.

Share this post


Link to post
Share on other sites

That won't work - you'll get an infinitely fractional (or however close the rounding of the engine makes it) less chance of striking the highest number than anything else :rolleyes:

Share this post


Link to post
Share on other sites
That won't work - you'll get an infinitely fractional (or however close the rounding of the engine makes it) less chance of striking the highest number than anything else :rolleyes:
please explain? (my brain is already too tired at this hour, don't get it)

Share this post


Link to post
Share on other sites
That won't work - you'll get an infinitely fractional (or however close the rounding of the engine makes it) less chance of striking the highest number than anything else :rolleyes:

Hehe... yeah. But I'll put that on the "it's-only-a-game-account".

Even though the nerd in me wants it perfect!

Share this post


Link to post
Share on other sites

Actually, I think it is right. I should just stay quiet for the next 2 1/2 hours, then I can go to bed. Out of curiousity, because of something only partially related - if you load the mission, then save it, then run the program, then load the saved game, then run the program - does it spin up a different number?

Share this post


Link to post
Share on other sites
That won't work - you'll get an infinitely fractional (or however close the rounding of the engine makes it) less chance of striking the highest number than anything else :rolleyes:

If you used round you would, but floor results in even distribution according to earlier examples and tests.

Share this post


Link to post
Share on other sites

If we put aside how the random number generator works, the chance for every whole number is the same:

floor(random(5)) is the same as floor(number between 0 and 4.99999, both included)

...so, to get 0 you need the random to return 0.0 to 0.9, and to get the highest number you need the result to be between 4.0 to 4.9 and both examples have the same range.

(not sure about the proper terminology here, but you know what i want to say)

Share this post


Link to post
Share on other sites

I love where this discussion is going! :)

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  

×