Raised This Month: $ Target: $400
 0% 

random(), random_num()


Post New Thread Reply   
 
Thread Tools Display Modes
siriusmd99
Veteran Member
Join Date: Oct 2013
Location: Republic of Moldova
Old 11-19-2017 , 05:10   Re: random(), random_num()
Reply With Quote #11

0,1 will do the thing for equal cantities. But you have 9 numbers and then 3 numbers. So the division is 9/3=3 (times)
Maybe you can do:

random_num(0,3) ? random_num(1, 9):random_num(12,14)

So if you have 0 it will generate number between 12 and 14 and if you have 1,2,3 it will generate numbers between 1 and 9

But I'm not sure 100%, it's just an idea.
Anyway I suggest you to do loop and repeat it until you get number different from 10 and 11.
Infinite loop can happen only if your excluded numbers are more than wanted ones. In this case you only exclude two numbers, so the probability that it will return 10 and 11 forever is just funny to think about.

Also when you add for example +1 when it's 11 it will increase the probability of number 12 by two times.
One time will be natural 12 and another time will be artificial 12 (11+1). And same for 10-1.

like this:
1,2,3,4,5,6,7,8,9,10-1,11+1,12,13,14
=>
1,2,3,4,5,6,7,8,9,9,12,12,13,14

I don't think this array looks right.

Last edited by siriusmd99; 11-19-2017 at 05:19.
siriusmd99 is offline
Depresie
Veteran Member
Join Date: Nov 2013
Old 11-19-2017 , 08:15   Re: random(), random_num()
Reply With Quote #12

There are more ways to "hack" this particular case.. but none of them would be more efficient than the method posted by fysiks..

Also it is the only method posted that doesn't screw the probabilities..
__________________
Depresie is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 11-19-2017 , 08:50   Re: random(), random_num()
Reply With Quote #13

Quote:
Originally Posted by Depresie View Post
There are more ways to "hack" this particular case.. but none of them would be more efficient than the method posted by fysiks..

Also it is the only method posted that doesn't screw the probabilities..
PRoSToTeM@'s method also works fine but only allows for skipping intervals of numbers
I assumed the OP gave a specific example but will probably need it for multiple scenarios
if you need to skip 10 and 12 but not 11 then you can't use that


also fysiks way is correct and probably the method used by games in time critical areas (such as chance based effects like critical damage, on every hit or shot) but needs to be hardcoded carefully


I stand by my version, not because of its efficiency, but because it's accessible and can accommodate many scenarios
Don't forget this is amxmodx help section, not C optimization discususion. Not saying these things shouldn't be explained so the users understand what is appropriate where but it's not the right topic











Should've started with this question first:
OP, what are you trying to achieve?
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose View Post
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.
aron9forever is offline
Natsheh
Veteran Member
Join Date: Sep 2012
Old 11-19-2017 , 09:10   Re: random(), random_num()
Reply With Quote #14

Quote:
Originally Posted by fysiks View Post
This still skews the probabilities.
No it wont since all has a half chance to be chosen.
__________________
@Jailbreak Main Mod v2.7.0 100%
@User Tag Prefix 100% done !
@Mystery Box 100% done !
@VIP System 100% done !

Natsheh is offline
Send a message via MSN to Natsheh Send a message via Skype™ to Natsheh
klippy
AlliedModders Donor
Join Date: May 2013
Location: Serbia
Old 11-19-2017 , 09:43   Re: random(), random_num()
Reply With Quote #15

Quote:
Originally Posted by Natsheh View Post
No it wont since all has a half chance to be chosen.
It wouldn't if your two intervals had the same number of elements.
klippy is offline
Airkish
AlliedModders Donor
Join Date: Apr 2016
Location: Lithuania
Old 11-19-2017 , 09:54   Re: random(), random_num()
Reply With Quote #16

Quote:
Originally Posted by aron9forever View Post
Should've started with this question first:
OP, what are you trying to achieve?
There's 14 skin cases.
Every player have a chance to receive any of these cases when kills enemy, except 2 of them (10, 11) which are only obtainable by completing missions.

So I need to make those 2 to not be included in kill drops.

P.S. I can't just make those 2 cases 13, 14.
__________________

Last edited by Airkish; 11-19-2017 at 09:55.
Airkish is offline
aron9forever
Veteran Member
Join Date: Feb 2013
Location: Rromania
Old 11-19-2017 , 10:30   Re: random(), random_num()
Reply With Quote #17

Quote:
Originally Posted by Airkish View Post
There's 14 skin cases.
Every player have a chance to receive any of these cases when kills enemy, except 2 of them (10, 11) which are only obtainable by completing missions.

So I need to make those 2 to not be included in kill drops.

P.S. I can't just make those 2 cases 13, 14.
so
- you will use the function quite often (on every kill)
- you probably won't be changing the cases that often
- you need this thing to be fair, otherwise as time goes by people will start racking up cases that drop more often
- you might need the flexibility to exclude other cases, maybe number 18, at some point in the future

based on this I would recommend fysiks' solution, store all the cases you want to be able to randomly select in an array, then randomly select one element of that array

it will look pretty ugly but you only need to do it once and you can do it at the top of the plugin, just make sure to add comments so you or other possible future developers know what is happening there. You don't want to look at the code again in 6 months and think "wtf was I doing here?"
__________________
Meanwhile, in 2050:
Quote:
Originally Posted by aron9forever
useless small optimizations
Quote:
Originally Posted by Black Rose View Post
On a map that is 512x512x128 units you end up with 3,355,443,200,000 different "positions". To store each one of those positions individually in the variable "user_or" you need 12 terabytes of memory.
aron9forever is offline
PRoSToTeM@
Veteran Member
Join Date: Jan 2010
Location: Russia, Ivanovo
Old 11-19-2017 , 12:07   Re: random(), random_num()
Reply With Quote #18

Quote:
Originally Posted by siriusmd99 View Post
Infinite loop can happen only if your excluded numbers are more than wanted ones.
It also can happen if random function is a constant xD.

Quote:
Originally Posted by aron9forever View Post
I assumed the OP gave a specific example but will probably need it for multiple scenarios
if you need to skip 10 and 12 but not 11 then you can't use that
You can just uncomment the commented block and change 11 to 12:
Code:
new num = random_num(1, 14 - 2); if (num >= 10) {     ++num;     if (num >= 12) {         ++num;     } }
__________________
PRoSToTeM@ is offline
Send a message via ICQ to PRoSToTeM@ Send a message via Skype™ to PRoSToTeM@
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 11-19-2017 , 14:00   Re: random(), random_num()
Reply With Quote #19

Quote:
Originally Posted by siriusmd99 View Post
Infinite loop can happen only if your excluded numbers are more than wanted ones.
That is not true. With true randomness, it is still possible to always get one of the excluded numbers. However, it's extremely unlikely which is why it usually doesn't cause significant issues. The most important part of this is that you will never know how many times your while loop will execute which is just bad code.

Quote:
Originally Posted by Natsheh View Post
No it wont since all has a half chance to be chosen.
No. The probabilities are skewed. The probability of getting a number from 1 to 9 is 5.555% (0.5 * 1/9) and the probability of getting a number from 12 to 14 is 16.666% (0.5 * 1/3).

Quote:
Originally Posted by PRoSToTeM@ View Post
You can just uncomment the commented block and change 11 to 12:
Code:
new num = random_num(1, 14 - 2); if (num >= 10) {     ++num;     if (num >= 12) {         ++num;     } }
So, what if you need to exclude 3, 8, 9, 15, 21, and 26? (Rhetorical Question) It's not scalable code. So, if you go with a more scalable solution now, then you wont' have to rewrite the [functional part of the] code in the future when you add more exclusions.
__________________

Last edited by fysiks; 11-19-2017 at 14:06.
fysiks is offline
KiLLeR.
Senior Member
Join Date: Jul 2014
Location: Bulgaria
Old 11-19-2017 , 17:13   Re: random(), random_num()
Reply With Quote #20

Quote:
Originally Posted by fysiks View Post
you will never know how many times your while loop will execute which is just bad code.
I thought that the idea for 'while loops' are to use em, when you don't know how many times they gonna be executed?!?
KiLLeR. is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 14:48.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode