Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


How do I change the code so that the app does not output the same facts every time?

I was wondering how I could change the code so that the app would not output the same facts every time, does anyone know how I could do this?

4 Answers

Sagar Suri
Sagar Suri
6,043 Points

Use this: Added in java 8

fact = ThreadLocalRandom.current().nextInt(0,mfacts.length);

Would you be able to copy and paste my code but corrected with your code? Thanks

Jack Crane
Jack Crane
2,491 Points

Could you provide a bit more detail as to which stage of the track you are on? There is a section where (if you are following the fact generator app) that it does randomly generate the fact.

Yes but sometimes the same facts repeat twice

I have finished the code and app and have done everything correctly but it's just that when I open the app and read the facts..sone seem to have repeated and I don't want it to do that. I only want it to output each of the facts once, every time you open the app

justin z
justin z
11,042 Points

Hi Marc, When you first initialize the mFact member variable in the FunFactsActivity, you can have the app start with a random fact instead of the default initial fact by initializing it with "mFactBook.getFact();" However its hard to say if I can't look at your code and see how far along you are in the app. Could you post your code?

Would you be able to edit my code and then post the corrected version on here? Thanks

package com.marc.funfacts;

import java.util.Random;

public class FactBook {
    // Fields Member Variables) - Properties about the object
    private String[] mfacts = {
            "Ants stretch when they wake up in the morning.",
            "There are more stars than there are grains of sand on Earth.",
            "There are more molecules in a cup of water than there are cups of water in the ocean.",
            "Ants don’t have enough mass to die on impact, no matter the height.",
            "With arms spread straight out, the distance from middle finger tip to middle finger tip is equal to your height.",
            "When you dream, everything you see in that dream, you’ve seen before in real life.",
            "Aladdin is Chinese.",
            "You can’t hum while holding your nose.",
            "There are more fake flamingos in the world than real flamingos.",
            "Carrots were originally purple.",
            "Humans share 50% of their DNA with bananas.",
            "There are more public libraries than McDonald’s in the U.S. ",
            "An octopus has three hearts.",
            "There is a basketball court on the top floor of the U.S. Supreme Court Building known as the “highest court in the land.”",
            "If a piece of paper were folded 42 times, it would reach to the moon.",
            "There are about twice as many nipples on Earth as there are people.",
            "Saudi Arabia imports camels from Australia.",
            "The name Jessica was created by Shakespeare in the play Merchant of Venice.",
            "It rains diamonds on Saturn and Jupiter.",
            "Every two minutes, we take more pictures than all of humanity did in the 19th century.",
            "Every two minutes, we take more pictures than all of humanity did in the 19th century.",
            "There’s only one hole in a straw.",
            "Alaska is simultaneously the most northern, the most western, and the most eastern state in the U.S.",
            "Vending machines are twice as likely to kill you as a shark is.",
            "Dolphins have names for one another.",
            "A strawberry isn’t a berry but a banana is.",
            "The probability of you drinking a glass of water that contains a molecule of water that also passed through a dinosaur is almost 100%.",
            "Cleopatra lived closer to the invention of the iPhone than she did to the building of the Great Pyramid.",
            "North Korea and Finland are separated by one country.",
            "Ostriches can run faster than horses.",
            "Olympic gold medals are acually mostly of silver.",
            "You are born with 300 bones; by the time you are an adult you will have 206.",
            "It takes about 8 minutes for the light from the Sun to reach Earth.",
            "Some bamboo plants can grow almost a meter in just one day.",
            "The state of Florida is bigger than England.",
            "Some penguins can leap 2-3 meters out of the water.",
            "On average, it takes 66 days to form a new habit.",
            "Mammoths still walked the Earth when the Great Pyramid was being built."};
    //Methods - Actions the object can take
    public String getFact() {
    String fact = "";
    // Randomly select a fact
    Random randomGenerator = new Random();
    int randomNumber = randomGenerator.nextInt(mfacts.length);
    fact = mfacts[randomNumber];

    return fact;