Question

3. Develop a random number generator that generates two-digit decimal numbers where individual digits appear with the following probabilities: digit 1 2 3 probability 0.1 0.2 0.3 0.4 For example, the number 43 (and 34) will be generated in 12% of cases, while l I will appear only in 1 % of cases.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Random;

public class RandomGenerator {

    private Random random;

    public RandomGenerator() {
        random = new Random();
    }

    public int getNextInt() {
        return (10 * generateDigit()) + generateDigit();
    }

    private int generateDigit() {
        double value = random.nextDouble();
        if(value <= 0.1) {
            return 1;
        } else if(value <= 0.3) {
            return 2;
        } else if(value <= 0.6) {
            return 3;
        } else {
            return 4;
        }
    }

}
public class RandomGeneratorTest {

    public static void main(String[] args) {
        RandomGenerator randomGenerator = new RandomGenerator();
        for(int i = 0; i < 10; ++i) {
            System.out.println(randomGenerator.getNextInt());
        }
    }

}

14 34 42 23 24 42 Process finished with e xit code 0

Add a comment
Know the answer?
Add Answer to:
3. Develop a random number generator that generates two-digit decimal numbers where individual digits appear with...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT