Percent Chance

Post » Fri May 27, 2011 1:25 pm

So I'm trying to use OBSE's rand function to create a chance.

I have a portion of math that spits out a percent (so it could be anything between 0 and 100). But translating that percent into an actual chance is baffling me.

If i were to do:
set chance to rand 1 2if (chance < 1.5)  ; 50% chance that this will runendif


That makes a 50% chance, but it only does that. How could I use variables to make this accept any percent, and make the code in the if block run only X% of the time?

My feeble mind can't wrap around this math, so if someone else more gifted at this could help me that would be great :D
User avatar
Chantelle Walker
 
Posts: 3385
Joined: Mon Oct 16, 2006 5:56 am

Post » Sat May 28, 2011 2:26 am

Might not be directly useful, but any reason you can't use ( GetRandomPercent + 1 )?

Alternatively, could you not just use a min/max of 1 and 100?

The only other thing I can think of is whether you have Chance as a Float? If it's a short, it auto-rounds to 1, which would probably cause what you're seeing?
User avatar
Anna Kyselova
 
Posts: 3431
Joined: Sun Apr 01, 2007 9:42 am

Post » Fri May 27, 2011 9:39 pm

The GetRandomPercent function (as with all of the other random ones) are really, really squishy. If you run it on "Begin GameMode" it'll refresh it every frame (and if you're running at several frames per second - which you are, and it'll be likely to be between 20-50 FPS). This means it makes your supposedly rare occurrence happen about every 3 seconds (if it's meant to be only 1% chance out of the random output). It's slightly more complicated, but you'll have to tell it when you want a new random percent (such as using it on a OnActivate block, or using lots of variables to update it given something happens).
Also, I don't understand the use of http://cs.elderscrolls.com/constwiki/index.php/Rand? Surely using http://cs.elderscrolls.com/constwiki/index.php/GetRandomPercent does exactly the same (without the hassle of floats) and with easier manipulation. Surely if you want a value of between 1 and 2; you could use GertRandomPercent (which makes a number - integer - between 0 and 99) * 0.01 + 1.
Given that the RandomPercent gives a value of X, then the outputs are:
  • X = 000; Output = 000 * 0.01 + 1 == 1 (Min)
  • X = 050; Output = 050 * 0.01 + 1 == 1.5 (I'd like to say it's the average value, but it won't be for some obscure reason...)
  • X = 100; Output = 100 * 0.01 + 1 == 2 (Max)
That shows that it will output a value between 1 and 2. More examples are on the http://cs.elderscrolls.com/constwiki/index.php/GetRandomPercent page (linked).

Good luck; and I hope that helped (but I don't think I understood the question completely. Sorry if that's the case!).
User avatar
Robert Bindley
 
Posts: 3474
Joined: Fri Aug 03, 2007 5:31 pm

Post » Fri May 27, 2011 7:26 pm


The main difference between rand and getrandompercent is the way in which the random value is acquired. Rand is more random and has no particular biases while getrandompercent tends to "roll" numbers between 30 and 70 more often than not. For simple checks when you don't need any specific change of occurance, this isn't that much of an issue, but when you have scripts which really must work on a true 1/5 chance or less of something happening, it can leave you wanting.
User avatar
Dalton Greynolds
 
Posts: 3476
Joined: Thu Oct 18, 2007 5:12 pm

Post » Fri May 27, 2011 8:41 pm

Alternatively, could you not just use a min/max of 1 and 100?

That quite simply solves this whole problem :D ! I just realized I could do that instead and do something like this:

; code that assigns perc to a percent value (between 1 and 100)set chance to rand 1 100if (chance < perc); the bigger perc is, the bigger the chance this will runendif


Thanks Kengon for that extensive explanation, but I'd rather use rand because I need a true random number, and like Vagrant said, its not exactly completely random. And no offense or anything but your explanation confuses me :ahhh:
User avatar
Ashley Hill
 
Posts: 3516
Joined: Tue Jul 04, 2006 5:27 am

Post » Sat May 28, 2011 12:40 am

The main difference between rand and getrandompercent is the way in which the random value is acquired. Rand is more random and has no particular biases while getrandompercent tends to "roll" numbers between 30 and 70 more often than not. For simple checks when you don't need any specific change of occurance, this isn't that much of an issue, but when you have scripts which really must work on a true 1/5 chance or less of something happening, it can leave you wanting.


Is there any proof? I never noticed anything like that. How big is the variance?
User avatar
Mandy Muir
 
Posts: 3307
Joined: Wed Jan 24, 2007 4:38 pm

Post » Sat May 28, 2011 1:18 am

There is some talk about it on CSWiki...

http://cs.elderscrolls.com/constwiki/index.php/Talk:GetRandomPercent
User avatar
Elisabete Gaspar
 
Posts: 3558
Joined: Thu Aug 31, 2006 1:15 pm

Post » Fri May 27, 2011 10:57 am

The indented stuff is basically irrelevant. Skip to the EDIT bit.
If you mean the bit that goes:

short dice
set dice to 6.0/99 * GetRandompercent

Is the loaded variant and returns this:
int(6/99 * 00) = int(0.00) = 0
int(6/99 * 16) = int(0.97) = 0 => Chance: 17%
int(5/99 * 17) = int(1.03) = 1
int(5/99 * 32) = int(1.94) = 1 => Chance: 16%
int(5/99 * 33) = int(2.00) = 2
int(5/99 * 49) = int(2.97) = 2 => Chance: 17%
int(5/99 * 50) = int(3.03) = 3
int(5/99 * 65) = int(3.94) = 3 => Chance: 16%
int(5/99 * 66) = int(4.00) = 4
int(5/99 * 82) = int(4.97) = 4 => Chance: 17%
int(5/99 * 83) = int(5.03) = 5
int(5/99 * 98) = int(5.94) = 5 => Chance: 16%
int(5/99 * 99) = int(6.00) = 6 => Chance: 1%

Then that's because it's not truely to 1 decimal place, since it's much more reliant on the value at 2 or 3 decimal places (EDIT Yeah, this bit makes basically no sense too, but what I'm trying to say is something like "it's a much more complicated and intricate way of doing things"). Since it always rounds down (http://cs.elderscrolls.com/constwiki/index.php/Ceiling_and_Floors it), the chance of getting a 6 is minute. And in this, 6 is the very upper limit - unlike in the function where it is "* 6 + 1" (the one above this confusing one) where the very upper limit is 6.99. Thus it's not really a very relevant comparison to use. That's because they're not even doing the same things. It might be if you were trying to find an even way of finding 0-5, but you're not.
I'm no expert, and I've not got enough time or patience to test the GetRandomPercent, but I'm fairly sure it's unbiased. Saying that, maybe I'm the biased one by fighting my corner.

Also, sorry for the confusing post(s). It made sense to me, but that's not really what counts! I'll work on my mathmatical explaination skills. ;)
I hope that made sense?

EDIT: Okay. Now I realise that's not even what you were talking about. I'm presuming you meant the "Should add that Ian's point was, GetRandomPercent is not especially random. Use rand instead if that is important" part, after some confusing maths talk. My bad. I don't understand what Haama wrote, so... You win. xD
User avatar
Far'ed K.G.h.m
 
Posts: 3464
Joined: Sat Jul 14, 2007 11:03 pm

Post » Sat May 28, 2011 12:19 am

Yes, that is my problem. The page on the Wiki doesn't say why 'it is not especially random'. I have no idea what

That function is a wrapper around MS' stdlib rand that calls srand(time(NULL)) if it hasn't been seeded yet. It only uses the system timer once. The rest of the code is just some modular arithmetic done in fixed point.
:turtle:

means. All I know is that rand seems to use a different and probably more accurate algorithm (Mersenne Twister algorithm!!!11!!) than getrandompercent to determine the random number. But what is the problem with getrandompercent? How big is the variance? If I use 'if getrandompercent <= 29' will I get only 29.1% 'real' chance? Or 29.5%? Or 29.9%? Or 29.999%? Is the difference so big that you would actually notice it or is it just a theoretical difference? Like 0.01% in the worst case? I assume it's is only theoretical, so I wonder in what situation you would need an absolutely exact algorithm up to last decimal place when you look for a random percentage anyway.
User avatar
J.P loves
 
Posts: 3487
Joined: Thu Jun 21, 2007 9:03 am

Post » Fri May 27, 2011 1:14 pm

As you said, what makes random "random"? Surely it's just chance that dictates whether your random number does or does not meet your criteria. It's not as if (given the chance of rolling a 6 is 1/6) you have to roll 6 times to see a 6. I can't imagine that the variance is all too significant or even important given that no really important maths that relies on it is going to be used. If you want exactly, say, 1 in 5 monsters to be enabled in a dungeon per dungeon clear out, why not just put in a levelled monster list which spawns a fifth less monsters?

Again, a pretty incomprehensible reply from me, but excuse me; I think I've got glandular fever. :shrug:

EDIT: That's post 101 from me. Got to mean something, right? :celebration:
User avatar
Toby Green
 
Posts: 3365
Joined: Sun May 27, 2007 5:27 pm


Return to IV - Oblivion