Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

Java 

How to make something happen randomly 'n' out of 'n' times?

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
    }
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?
(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.
(Feb 5, 2017 04:07 PM)matt 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/11552...-something
(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/11552...-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.
(Feb 5, 2017 06:17 PM)matt 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.
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
}
I don't middleman any deals on- or off-site. Please don't message me requesting my past services and certainly don't fall for anybody who claims to be me.
(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


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C#] by teeftoofMar 23, 2016 04:50 PM 6 277 Mar 23, 2016 04:50 PM
Last Post: Oliver Sykes
[Shell] by MaxPainDec 16, 2010 02:24 PM 3 47 Dec 16, 2010 02:24 PM
Last Post: dumbas13