7 Replies Latest reply on Mar 11, 2014 9:05 AM by resdesign

    Random numbers in a text

    yaotekatl52

      Hi all,

       

      how can I get a Random Number in a Text by clicking a button?

       

      For example: I have an already animated object [a dice] as button and I have a Text object located on other part of the stage and where should appear the random number from 1 to 6.

       

      I have tried in CreationComplete and compositionReady, and in the Click option in the button, one of these two options:

       

      Math.floor((Math.random()*6)+1);

      var ran_number=Math.floor(Math.random()*5;

       

      I have also tried in the dice image [button] within the symbol this [with one of the two previous codes]:

       

      sym.getComposition().getStage().$("text").html(ran_number);

       

      And it doesn't work anyway. Right now I'm basically lost.

       

      Can anyone help me with this issue? [considering I'm a grown child-man, which means that help would be like: 1.- start your computer. 2.- open Edge. 3.- etc etc etc]  ;-)

       

      Many, many thanks in advance.

       

      Armando.

        • 1. Re: Random numbers in a text
          resdesign Adobe Community Professional & MVP

          This worked for me:

           

          sym.$('btn').click(function(){

                    var ran_number = Math.floor((Math.random()*6)+1);

                    sym.$("text").html(ran_number);

           

          });

           

          Make sure your text is named text and not Text in the element panel.

           

          see file: https://www.box.com/s/h4t7sz8n7vhtpbhms747

          • 2. Re: Random numbers in a text
            yaotekatl52 Level 1

            Hi Resdesign!!!

             

            Thank you very, very, very much.

             

            I have tried your advice and it works!! I had been with this issue for almost three whole days, consulting different sources and trying a lot of diverse codes and combinations...

             

            I have to deliver this work [I'm completely new with HTML and Java] and since Flash is directing straight forward to the Forgotten Land I have to recicle myself.

             

            You just saved my neck.

             

            Thanks again and very kind regards.

             

            Armando.

            • 3. Re: Random numbers in a text
              resdesign Adobe Community Professional & MVP

              My pleasure! Now go help someone else!

              • 4. Re: Random numbers in a text
                heathrowe Most Valuable Participant

                Nice one Marie

                • 5. Re: Random numbers in a text
                  resdesign Adobe Community Professional & MVP

                  Thank you Darrell!

                  • 6. Re: Random numbers in a text
                    mrbor

                    This is really great! 

                     

                    Can you help me expand on this a bit please?

                     

                    I am trying to create an 8 digit random number (that must be no less that 8 digits).

                     

                     

                     

                    sym.$('btn').click(function(){

                     

                        var ran_number = Math.floor((Math.random()*100000000)+1);

                     

                        sym.$("Text").html(ran_number);

                     

                     

                    });

                     

                     

                    I figured that by simply using what you've supplied it'd just work but I've gotten 7 digit numbers as a result (every 10 clicks or so).

                     

                     

                    In the end, I'd like to have a person input a numerical password that either matches or doesn't match the random number.

                     

                    Is there a way to create each integer one at a time and then when the user enters his password, have Edge check each integer one at a time?

                     

                    I hope that makes sense!

                     

                    Thank you!

                    • 7. Re: Random numbers in a text
                      resdesign Adobe Community Professional & MVP

                      I have 8 text fields with the names included in the array:

                      var numbers = ['onePlace','twoPlace','threePlace','fourPlace','fivePlace','sixPlace','sevenPlace','eigh tPlace'];

                      Then I have this for loop to randomize the numbers.

                          for(i=0;i<numbers.length; i++){

                                var ran_number = Math.floor((Math.random()*9));

                                sym.$(numbers[i]).html(ran_number);

                       

                      sample here:

                      https://app.box.com/s/77krg9n5361uxuyvos3o

                       

                      This below is not pretty but it works - it is for one text field.

                       

                      for(i=0;i<9; i++){

                      var number1 = Math.floor((Math.random()*9));

                      var number2 = Math.floor((Math.random()*9));

                      var number3 = Math.floor((Math.random()*9));

                      var number4 = Math.floor((Math.random()*9));

                      var number5 = Math.floor((Math.random()*9));

                      var number6 = Math.floor((Math.random()*9));

                      var number7 = Math.floor((Math.random()*9));

                      var number8 = Math.floor((Math.random()*9));

                      sym.$('myNumber').html(number1+' ' +number2+' ' +number3+' ' +number4+' ' +number5+' ' +number6+' ' +number7+' ' +number8);

                      }

                       

                      This would make it easy to compare the input with each number.