• To whom it may concern,

    I’m making a quiz with your random number generator. Is there a way to make sure the same random number is used throughout a question. I want the question number and the answer number to be the same. For example below.

    How many grams are there in a [arandomnumber min=1 max=10000] pound sample?

    The answer is [calculate][arandomnumber min=1 max=10000]*454[/calculate]

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • Hi,

    Unfortunately, this isn’t possible with the plug-in. By default, the shortcode outputs a random number each time it’s called. This would require updating the plug-in to set an identifier so a number can be reused again. I like the idea, but I don’t currently have the time to update it.

    One idea I can think of off the top of my head would be the wrap the shortcode in a span tag, then use jQuery to grab the number that is generated, and paste it between a different span tag. I did a quick test and the below worked for me.

    HTML

    Here is my first random number: <span class=”n01″>[arandomnumber min=1 max=10000]</span>
    Here is my second random number: <span class=”n02″>[arandomnumber min=1 max=100]</span>

    Here is the first number again: <span class=”n01a”></span>
    Here is the second number again: <span class=”n02a”></span>

    JS

    jQuery(document).ready(function() {
    	jQuery( ".n01" ).clone().appendTo( ".n01a" );
    	jQuery( ".n02" ).clone().appendTo( ".n02a" );
    });

    Class names can be changed to anything else, and you can add as many as you like. The JS code needs to be placed in a custom.js file or wrapped in a script tag and placed in a field for custom code, if your theme has one.

    This will always take the value of whatever is within the n01 span tag and append it to the n01a span tag. Each time the page reloads the numbers will change, but n01 will always match n01a, etc.

    I hope this is helpful!

    • This reply was modified 5 years, 6 months ago by macardam.
    • This reply was modified 5 years, 6 months ago by macardam. Reason: formatting
    Thread Starter chemistryeducation4

    (@chemistryeducation4)

    Hello,

    I’m new to javascript and website development. I added the custom.js script to my server in wp-admin and wp-includes js file. It is still not reporting. Not exactly sure where to put the js file. There are alot of different customize js files on the server. Any step by step directions will be great.

    I recommend adding a plug-in such as https://wordpress.org/plugins/head-footer-code/ that can allow you to easily add js code to your site.

    After activated, you can then add the following to the FOOTER Code option:

    <script>
    jQuery(document).ready(function() {
    	jQuery( ".n01" ).clone().appendTo( ".n01a" );
    	jQuery( ".n02" ).clone().appendTo( ".n02a" );
    });
    </script>

    You can change n01/n01a, and n02/n02a to whatever you prefer, or add more lines here as needed.

    Then, following your example above, you can use:


    How many grams are there in a <span class=”n01″>[arandomnumber min=1 max=10000]</span> pound sample?

    The answer is [calculate]<span class=”n01a”></span>*454[/calculate]

    Let me know if that works for you.

    Thanks!

    However, I’m not sure if this will work with the [calculate] shortcode. What plug-in are you using for that?

    Thread Starter chemistryeducation4

    (@chemistryeducation4)

    I’m still having trouble with the repeating number. The plugin I’m using is Calculate Values with Shortcodes from wordpress that seems to give me an error.

    Warning: unexpected operator ‘*’ in /home5/gopro/public_html/wp-content/plugins/calculate-values-with-shortcodes/evalmath.class.php on line 360
    How many grams are there in a 628 pound sample?

    The answer is 0

    Thread Starter chemistryeducation4

    (@chemistryeducation4)

    I typed in the code exactly like below in the page window.
    How many grams are there in a <span class=”n01″>[arandomnumber min=1 max=10000]</span> pound sample?

    The answer is [calculate]<span class=”n01a”></span>*454[/calculate]

    Then scrolled down and placed the java script in the footer code section. I also tried going to tools and placing it there in the footer code section.

    https://chemmatter.org/test-question/

    Thread Starter chemistryeducation4

    (@chemistryeducation4)

    Is there any other way to randomize the numbers?

    You’re probably receiving the error because you’re pasting the code into the Visual tab of the text editor instead of the Text tab. Also, the replies here may be adding formatted quotations. It should look like this:

    How many grams are there in a <span class="n01">[arandomnumber min=1 max=10000]</span> pound sample?
    
    The answer is [calculate]<span class="n01a"></span>*454[/calculate]

    Unfortunately, the [calculate] shortcode won’t work with this solution. If I can find some time, I can try to update the plug-in to allow for reusing numbers, but at this point I’m not sure there is anything else I can suggest. This plug-in was created to be very simple and output a random number each time it was called, so it’s probably not the best option for more complex solutions like this.

    Thread Starter chemistryeducation4

    (@chemistryeducation4)

    You’re right it seems to still give a solution of zero. If you find a way to repeat the same random number please let me know. I also found this snippet of code for generating random numbers. Is there a way to assign the number to be allowed in the calculate? This function works [calculate][arandomnumber min=1 max=100][/calculate].

    <h2>JavaScript Math</h2>

    <p>Math.floor(Math.random() * 100) + 1) returns a random integer between 1 and
    100 (both included):</p>

    <p id=”demo”></p>

    <script>
    document.getElementById(“demo”).innerHTML =
    Math.floor(Math.random() * 100) + 1;
    </script>

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Random number generator’ is closed to new replies.