Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / How to code Probability?

Author
Message
Sakuya
17
Years of Service
User Offline
Joined: 16th Feb 2009
Location: IB*ET
Posted: 10th Apr 2010 17:32
Ive been searching now for at least 1 hour through Google and can't really find something usefull.

What I try to achieve is a Probability Function like for RPG Games where the Character moves around and suddenly enters a Battle.

I set the Probability for an Encounter in a Script like this:

The problem's that I just can't imagine how to code the Probability System like:

I have 37% chance to encounter an Enemy / enter a Battle.
dark coder
23
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 10th Apr 2010 17:34
By using a random number generator! DBPro comes with one that can be used via the 'rnd()' command.

Sakuya
17
Years of Service
User Offline
Joined: 16th Feb 2009
Location: IB*ET
Posted: 10th Apr 2010 17:42
well yes Im using the rnd but how exactly am I to use it?
something like this?

that would give me for the 37% example a random number between 1 and 37 if I didnt write something wrong

but how do I do the checking?
like, what do I do with that random number?

I think Ive got a wrong idea here...
dark coder
23
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 10th Apr 2010 17:56
More like: encounter = rnd(99) < percentageChance

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 10th Apr 2010 17:58
If you want a 37% chance of doing something then you can do something like



This works because rnd(99) returns random values from 0 to 99, i.e. 100 different values. The condition is true when the value is any number from 0 to 36, i.e. 37 different values or 37%.

If you need fractional values just increase the number of digits, e.g. to get 37.3% use "rnd(999)<373" instead.

I'm afraid I didn't understand how your code was supposed to work.
Sakuya
17
Years of Service
User Offline
Joined: 16th Feb 2009
Location: IB*ET
Posted: 10th Apr 2010 18:19
thanks I really was on the wrong way...
sometimes I can't think straight :/

@Green Gandalf

yeah that was my Problem as I had no idea what to do so I tried some random stuff (I usually just try random stuff if I completely have no idea)
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 10th Apr 2010 18:23 Edited at: 10th Apr 2010 18:24
But this solution gives you a 37% chance of encountering an enemy per loop. Which means in a maximum of about 63 loops the enemy will come after you, and that is about 1 second.

If you want it to wait longer, you can check every 100 loops or so like this:



TheComet

Sakuya
17
Years of Service
User Offline
Joined: 16th Feb 2009
Location: IB*ET
Posted: 10th Apr 2010 18:26
well Im checking not the Encounter based on loops but on Movement.
If I move 1 Tile it checks if there will be an Enemy Encounter
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 10th Apr 2010 18:29
Oh, ok. Sounds good

TheComet

dark coder
23
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 10th Apr 2010 19:15
Quote: "Which means in a maximum of about 63 loops the enemy will come after you"


Where did you learn probability?

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Apr 2010 01:11
It's just as well we don't refer to "permega" instead of "percent" - if we did then he'd have to wait a maximum of about 630000 loops.
NickH
18
Years of Service
User Offline
Joined: 19th May 2008
Location: Nova Prospekt, North Yorks, UK
Posted: 11th Apr 2010 02:53 Edited at: 11th Apr 2010 03:40
(Removed...doesn't matter...sleep posting again )
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 11th Apr 2010 13:10
Quote: "Where did you learn probability?"


From this guy:



TheComet

Mnemonix
23
Years of Service
User Offline
Joined: 2nd Dec 2002
Location: Skaro
Posted: 11th Apr 2010 18:52
I calculate a 82% probability that you didn't learn it from him.

Your signature has been erased by a mod because it's larger than 600x120
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 11th Apr 2010 20:10
OK, I screwed up. I meant 1 in 3 loops will make the enemy come after you. Is that correct?

TheComet

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 11th Apr 2010 23:50
I think 37 in 100 would be more correct - and that assumes you check the condition every loop.
Dia
21
Years of Service
User Offline
Joined: 16th Jan 2005
Location:
Posted: 12th Apr 2010 14:31
unless you have the means in place to stop rolling multiple encounters, if the probability is checked every loop, then the probability of getting AT LEAST 1 encounter is given by

E = 1 - (1-p)^n

1 loop, 37% chance of encounter
2 loops, 60.3% chance of encounter
....
5 loops, 95% chance of encounter
10 loops, 99% chance of encounter

etc etc etc

computer crashed once when I was spawing computer opponents to play each other once.... because I ended up trying to spawn a character about 20 times per second. didnt take long for me to run outa memory

This is not the Sig you are looking for....
veltro
User Banned
Posted: 12th Apr 2010 15:22
Quote: "1 loop, 37% chance of encounter
2 loops, 60.3% chance of encounter
....
5 loops, 95% chance of encounter
10 loops, 99% chance of encounter"


I don't think so

Each chanche of encounter is indipendent from previous chanches: so for each step the probability is always 37%
Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 12th Apr 2010 16:41 Edited at: 12th Apr 2010 16:41
Quote: "Each chanche of encounter is indipendent from previous chanches: so for each step the probability is always 37% "


True - and not the issue.

Dia has correctly given the chance of an encounter by the end of the nth loop - assuming independence between loops. Obviously an encounter will eventually occur - just as you will eventually throw a "head" when tossing a fair coin.
Hawkblood
16
Years of Service
User Offline
Joined: 5th Dec 2009
Location:
Posted: 12th Apr 2010 18:31
You may want to change the random seed each time they move (use time to do that). That way it seems more random.

The fastest code is the code never written.
veltro
User Banned
Posted: 12th Apr 2010 18:59
Quote: "
Dia has correctly given the chance of an encounter by the end of the nth loop - assuming independence between loops. Obviously an encounter will eventually occur - just as you will eventually throw a "head" when tossing a fair coin. "


This is correct. Sorry I was misunderstanding Dia post.

Login to post a reply

Server time is: 2026-07-26 14:30:50
Your offset time is: 2026-07-26 14:30:50