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 / random numbers but giving higher priority to others?

Author
Message
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 14th Nov 2012 22:18
Suppose I wanted a random number between 10 and 50. Easy enough. But what if I wanted 10 to be more likely and 50 the least likely. So the closer the number is to 50, the harder it is to roll that number. There's gotta be some kind of statistical probability equation I can use to do that.

"You're not going crazy. You're going sane in a crazy world!" ~Tick
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 15th Nov 2012 01:46
I am glad you asked, I think I need this soon.

So this is what I figured. It uses a fortune parameter which controls how likely you are to get a number at either end of the scale, but this could be refined as it is 10 minutes work.



MrValentine
AGK Backer
15
Years of Service
User Offline
Joined: 5th Dec 2010
Playing: FFVII
Posted: 15th Nov 2012 10:41
This is interesting... Shall follow for now

GregA
15
Years of Service
User Offline
Joined: 21st Jun 2011
Location:
Posted: 15th Nov 2012 11:19
Dice statistics are your friend.

If you roll 2d6 you will roll 7 about half the time, because half of the various ways the dice can land will add up to 7. The results 2 and 12 are most rare because there are the least possible ways to get to 2 and 12.

So in your case, roll 10d10, which will give you a huge probability of rolling between 45 and 55, and you will almost never get 10 or 100 (a random result of all 1's or all 10's)

Then its late, and im loaded on coffee and don't feel like doing the arithmetic at the moment, but flip the numbers around and do some subtraction, and you should have your 10 to 50 value, with 50 almost never coming up.
basjak
16
Years of Service
User Offline
Joined: 16th Apr 2010
Location: feel like signing up for mars
Posted: 15th Nov 2012 11:22
This is very easy. I will use the easiest method:

EXP:

-you have five numbers: 1 - 2 - 3 - 4 - 5
-in ideal world, the possibility of picking each number = 1/5
-but we want:
number 1 to appear 4 times than the ideal 1/5
number 2 to appear 3 times than the ideal 1/5
number 3 to appear 2 times than the ideal 1/5
number 4 to appear 2 times than the ideal 1/5
number 5 to appear 1 time than the ideal 1/5
-now the total possibilities = 4+3+2+2+1 = 12
-let's create array:
dim possibility(12)
possibility(1) = 1
possibility(2) = 1
possibility(3) = 1
possibility(4) = 1
possibility(5) = 2
possibility(6) = 2
possibility(7) = 2
possibility(8) = 3
possibility(9) = 3
possibility(10) = 4
possibility(11) = 4
possibility(12) = 5

- call random number:
random_num = possibility(rnd(11)+1)

Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 15th Nov 2012 11:46 Edited at: 15th Nov 2012 11:52
@basjak

The random pool method is good, similar to what you suggested. It is best when you want to control how many times something must happen. When it happens, remove it from the array.

But when you want a large range of possibilities, of say 1 to 100000000, an array pool and your method is RAM overkill.

The CPU method that I posted would be better:

result = RandomChance(1, 100000, fortune)

You could also use ratios and floating point numbers if necessary.

Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 15th Nov 2012 12:59 Edited at: 15th Nov 2012 15:36
I guess you could use SIN, like:

V=10+abs((sin(rnd(360))*40))

[Edit]
or

V=10+(sin(rnd(90))*40)
V=10+(sin(rnd(900)/10.0)*40)
V=10+(sin(rnd(9000)/100.0)*40)

Depending on how much scope you need... if you need a bigger range of random numbers, then using the RND(900)/10.0 etc would increase the scope to suit.


That'll give a sorta bell-curve where the most likely value is 10, least likely is 50.

Might be enough to get the results your after, and would certainly cut down on code.

I got a fever, and the only prescription, is more memes.
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 15th Nov 2012 14:14
Quote: "V=10+abs((sin(rnd(360))*40))"


Yep, that's a good concise method.

Sergey K
22
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 15th Nov 2012 15:04 Edited at: 15th Nov 2012 15:05
how about working with percent chances?



so if you add value 1, it will give you a chance of 1 out of 100.

i belive this is what you looking for..

Advanced Updater for your games!
Mr909
14
Years of Service
User Offline
Joined: 2nd Jun 2012
Location:
Posted: 15th Nov 2012 18:38
Well, the Gaussian frame of mind is better reflected by my dice-rolling friend above, Greg A.

I actually did a probability distribution curve model showing precisely this. The best way to do random numbers on an even frequency distribution is a bell curve.

Here's my original program, UP polls 100 more data for a frequency distribution curve. The cyan one is 10d10, second is 5d20, third is 20d5. I did have an exclusionary system going where the Gaussian generator would drop, say, the bottom two, and that had an effect of regulating and increasing my results (a mean result became more frequent, the mean became higher).



My other method, and, you can yell at me for poor code structure until you're blue in the face and I won't care, is actually the same system I used for the name generator code. It reflects even statistic-based weight where you can increase odds by an amount relative to every other instance chance just by toying around with that attached .ini, copying the same number over and over. You could easily do the same with an array.

Best of luck Phaelax, I hope I helped.
Chris Tate
DBPro Master
17
Years of Service
User Offline
Joined: 29th Aug 2008
Location: London, England
Posted: 15th Nov 2012 19:05 Edited at: 15th Nov 2012 19:06
Quote: "function PercentChance(ChanceNum as Integer)
res as integer
res = RND(100)+1
if res <= ChanceNum then Res = 1 else Res = 0
endfunction Res"


Yours is a good method for a to hit chance Sergey; but features only two results, 1 or 0. We need multiple results; IE: number cards in a pack, number of sides on a dice, level of loot to spawn.
We also want high numbers to be possible; just unlikely.

My code will give you item N to item Z in the pack; but you might rearly pickup item A to M.

VanB's idea will give you item N to item Z in the pack; you will not get items A to M.

Sergey K
22
Years of Service
User Offline
Joined: 4th Jan 2004
Location:
Posted: 15th Nov 2012 19:16
Quote: "Yours is a good method for a to hit chance Sergey; but features only two results, 1 or 0. We need multiple results; IE: number cards in a pack, number of sides on a dice, level of loot to spawn.
We also want high numbers to be possible; just unlikely."


thanks, but i was planning for Phaelax to understand the method i was doing to annolize it and get a better idea to do what he exactly needs.

i expect no less for 2003 dbp programmer

Advanced Updater for your games!
Phaelax
DBPro Master
23
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 15th Nov 2012 20:49
I will have a need for Sergey's method, I just haven't gotten to that stage yet.

I need a range of numbers, for rolling random stats on items. I don't want a perfect item to have the same chance to roll as a really crappy one.

I'll be looking into this dice method.

"You're not going crazy. You're going sane in a crazy world!" ~Tick

Login to post a reply

Server time is: 2026-07-08 15:14:22
Your offset time is: 2026-07-08 15:14:22