Hack Forums
[Java] How to make something happen randomly 'n' out of 'n' times? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [Java] How to make something happen randomly 'n' out of 'n' times? (/showthread.php?tid=5543912)



How to make something happen randomly 'n' out of 'n' times? - matt - Feb 5, 2017

It's a little embarrassing asking for help because I'm a pretty good programmer but unfortunately, I'm not that great at math, and so here's what I came up with. I know it's not the most efficient code so I would like to know if there's some other kind of alternative. :/

Code
    public static void main(String[] args) {
        Random random = new Random();
        int numerator = 5, denominator = 100;
        boolean succeed = false;
        for(int i = 0; i < numerator; i++) {
            int chance = random.nextInt(denominator);
            if(chance == 0) {
                succeed = true;
                break;
            }
        }
                //do something if succeeded
    }



* - Stealth™ - Feb 5, 2017

You'll have to explain exactly what you're trying to do. From the code that you've posted, I can't even decipher what your intentions are. Are you wanting something to happen at a certain % chance?


* - matt - Feb 5, 2017

(Feb 5, 2017 03:41 PM)Stealth™ Wrote:  You'll have to explain exactly what you're trying to do. From the code that you've posted, I can't even decipher what your intentions are. Are you wanting something to happen at a certain % chance?

Yes pretty much, but I'm trying to represent it as a fraction.


* - Stealth™ - Feb 5, 2017

(Feb 5, 2017 04:07 PM)Midnight Blue™ Wrote:  Yes pretty much, but I'm trying to represent it as a fraction.

You should always check StackOverflow before bothering to make a thread, any question you have was probably answered years ago :P

http://stackoverflow.com/questions/11552158/percentage-chance-of-saying-something


* - matt - Feb 5, 2017

(Feb 5, 2017 05:44 PM)Stealth™ Wrote:  You should always check StackOverflow before bothering to make a thread, any question you have was probably answered years ago :P

http://stackoverflow.com/questions/11552158/percentage-chance-of-saying-something

I've come across this topic already, but it's not quite what I'm trying to do.
I have a numerator and a denominator and I want the numerator amount of rolls whilst the max value is the denominator and I want to refrain from changing it from a fraction to a percentage.


* - Stealth™ - Feb 5, 2017

(Feb 5, 2017 06:17 PM)Midnight Blue™ Wrote:  I've come across this topic already, but it's not quite what I'm trying to do.
I have a numerator and a denominator and I want the numerator amount of rolls whilst the max value is the denominator and I want to refrain from changing it from a fraction to a percentage.

Any fraction can be represented as a percentage though.


* - minimalist - Feb 5, 2017

Am I doing this wrong or is it not as trivial as you're making it out to be?

Code
int numerator = 1;
int denominator = 8;
boolean happen = false;

Random num = new Random();
int gen = num.nextInt(denominator) + 1;

if (gen > 0 && gen <= numerator) {
    happen = true;
}

if (happen) {
    // do something
}



* - matt - Feb 5, 2017

(Feb 5, 2017 07:16 PM)Minimalist Wrote:  Am I doing this wrong or is it not as trivial as you're making it out to be?

Code
int numerator = 1;
int denominator = 8;
boolean happen = false;

Random num = new Random();
int gen = num.nextInt(denominator) + 1;

if (gen > 0 && gen <= numerator) {
    happen = true;
}

if (happen) {
    // do something
}

Damn that was so simple lol smh thanks man this works fine :-)
I'm so dang bad at math my gosh