Comments (6)
I would like to help with this
from hag.
What will be the constants for the probabilities. Can you provide example?
from hag.
Also, random number generated should be both 0 and max inclusive?
from hag.
Yes, it should return in the range [0, max]
.
from hag.
Can you better explain the last two paragraphs?
from hag.
rng_rand should be sure to account for the limitations of rand in C. Namely, if the argument to rng_rand is greater than RAND_MAX but less than INT_MAX, it should piece together the result from multiple rand calls.
Say RAND_MAX
is 32767
, which is the lowest number it is allowed to be. Now say we want something to happen 1 in 50000
times. If we do rand() % 50000
and check if the result is 0, it will be 1 in 32767
times instead, as rand()
will not return anything greater than RAND_MAX
. This is sad, because wanting something that rare is perfectly reasonable, and 50000
is nowhere near the maximum size 4 bytes can get you. A common solution is to then piece together the results of rand()
by using bit shifting to create a wider range of return values. This is not currently done, but is low priority, as almost everybody will be compiling with GCC, where RAND_MAX == INT_MAX
.
Further, rng_rand and rng_roll should both use rejection sampling to ensure that the randomness is uniform across their respective ranges. That is, rng_rand(max) should return every number in [0, max] with equal probability, and rng_roll(droprate) should always yield 1 with probability 1/droprate in the long run.
rand()
can return any value in [0, RAND_MAX]
This is problematic if we wanted, say, a uniformly random number in [0, 5]
(that is, one of 0, 1, 2, 3, 4, 5
, or what you'd expect from a fair die roll where the die was painted by a programmer) and RAND_MAX % 6 != 5
. To see why, consider if RAND_MAX was something small, like 9 (it can't be that low, but this is just an example).
In that case, rand()
could return 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
. If we do the usual thing of rand() % 6
to get our number, then this is our distribution of rolls:
Roll Value | Probability | rand() return values |
---|---|---|
0 | 2 / 10 | 0 or 6 |
1 | 2 / 10 | 1 or 7 |
2 | 2 / 10 | 2 or 8 |
3 | 2 / 10 | 3 or 9 |
4 | 1 / 10 | 4 |
5 | 1 / 10 | 5 |
As you can see, in this case, simply moding the result of rand()
by the number of elements in the range we want would lead to a biased distribution. One solution is to re-call rand if it returns something that is in that last "incomplete" section of return values, which in our case is anything above 5. Doing that, we would re-roll anytime we saw a 6 or greater, and every side of the die would have a 1/6
chance of being returned. This is what we want.
This was addressed in #110, so no need to worry about it anymore.
Finally, rng.h should define the constants for various probabilities.
There should be a header file for various constants used for things that happen with a chance.
For example, item.c
scales the maximum power of a weapon by 10*(floor_number + 1)
- this
10 here could be a constant in rng.h
such as RNG_WEAPON_POWER_SCALE
.
Or how main.c
checks luck against rand() % 20000
for a crit chance. That 20000
should be a constant with a name like RNG_CRIT_THRESHOLD
.
This is not done, and is of a sensible priority level.
from hag.
Related Issues (20)
- The enemy system sucks and should be rewritten HOT 1
- Demo system HOT 6
- Key operates on "in page" not on "in vision" HOT 2
- Generate floors at launch?
- Tidy up main HOT 1
- This is bad and not guranteed true, should be done a different way HOT 1
- Change enemy movement HOT 2
- Support capital movement
- Support fight to the death
- Support potion warnings HOT 3
- Enemy weaknesses are non-obvious
- Release on itch.io HOT 4
- Build fails on Mac
- Improve accessibility by allowing for printing of important information
- Compiling without -std=c89 fails because of usleep
- contributing.md filename lowercase
- Contributing.md missing details HOT 1
- Sometimes rapid development is hard when sticking to the c89 standard
- Player character symbol should change on death to make death visible across all panels HOT 2
- Decouple ncurses from everywhere
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from hag.