rubyshoes

Random quote generator repeats the same quotes, why?


Good day to everyone! There is a quote generator made with Shoes.

    @think.click do
        @noun_nominative.shuffle
        @base.shuffle
        @thought.replace(@base.sample)
    end

    @noun_nominative = 
        [
            "word1", "word2", "word3", "word4",
            "word5", "word6", "word7", "word8"
        ]
    @noun_accusative = 
        [
            "word1", "word2", "word3"
        ]
    @base =
        [
        @noun_nominative.sample.capitalize + "regular quote",
        "Regular quote" + @noun_nominative.sample,
        "Regular quote" + @noun_accusative.sample,
        "Regular quote" + @noun_accusative.sample,
        @noun_nominative.sample.capitalize + "regular quote",
        "And another one base for a quote"
        ]       

It simply replaces phrases in base array with random words from noun_nominative and noun_accusative, showing a new quote every time button "think" is clicked.

The program should make a brand new quote with every click, however, it keeps showing the same phrases which were generated once. How could I make it regenerate the quotes without reopening the program?

Thank you for answering!


Solution

  • You need to generate random quote on each click, rather than pre-generating them at app startup. This should be the laziest change to do what you want:

    def generate_random_quote(nominative, accusative)
      all_generated_quotes = [
        nominative.sample.capitalize + " - это всякий, кто стремится убить тебя, неважно на чьей он стороне.",
        "Иногда " + nominative.sample + " не попадает в " + accusative.sample + ", но " + nominative.sample + " не может промахнуться.",
        "Нет ничего труднее, чем гибнуть, не платя смертью за " + accusative.sample + ".",
        "Индивидуумы могут составлять " + accusative.sample + ", но только институты могут создать " + accusative.sample + ".",
        nominative.sample.capitalize + " - это тот человек, который знает о вас все и не перестает при этом любить вас.",
        "Трудно себе представить, что сталось бы с человеком, живи он в государстве, населенном литературными героями."
      ]
    
      all_generated_quotes.sample
    end
    
    @think.click do
      freshly_baked_random_quote = generate_random_quote(@noun_nominative, @noun_accusative)
      @thought.replace(freshly_baked_random_quote)
    end
    

    It is, of course, rather wasteful. It generates all possible quotes and discards all but one. If that's a concern, fixing that would be your homework. :)