Random figure with button - InfoPath Dev
in

InfoPath Dev

Random figure with button

Last post 08-31-2011 08:53 AM by Lockdown. 12 replies.
Page 1 of 1 (13 items)
Sort Posts: Previous Next
  • 08-26-2011 03:45 AM

    • Joern
    • Top 150 Contributor
    • Joined on 09-28-2010
    • Posts 79

    Random figure with button

    Hi All,

     unfortunately I am absolutely not familiar with programming in InfoPath.

    I want to have a button that generates a random integer between 0 and 10 and sets it in another field.

    Do you have any idea how I can do this in IP 2003?

     

    Thank you very much in advance for your efforts.

     

    Cheers,

    Joern

  • 08-26-2011 07:36 AM In reply to

    Re: Random figure with button

    Are these browser enabled forms?

    You can accomplish this with one line of code actually.

    It would look something like this in Jscript.

    XDocument.DOM.selectSingleNode("/myFields/ID").text = Math.random()*10;

     That will give you a decimal point between 0 and 1. You will have to round it or format it as an integer.

    XDocument.DOM.selectSingleNode("/myFields/ID").text = Math.round(Math.random()*10);

    That should be all the code you need for a random number! In C# it will be almost identical. You can take that route if you have browser enabled forms or really want to learn some C#.


  • 08-28-2011 10:44 PM In reply to

    • Joern
    • Top 150 Contributor
    • Joined on 09-28-2010
    • Posts 79

    Re: Random figure with button

    Thanks for your response.

    Well, it's a form that the user can download from Sharepoint. Then, he's sending his input via e-mail to me.

    I assumed to put code in the Microsoft Script editor. And how do I put the random integer in a certain field, let's say in field1?

     Thanks in advance for your effort.

  • 08-29-2011 09:48 AM In reply to

    Re: Random figure with button

    XDocument.DOM.selectSingleNode("/my:myFields/my:field1").text = Math.round(Math.random()*10);

     The part in quotes is the Xpath of your field. So just right click your field1 element on the right task pane and select 'Copy Xpath", then place it in the quotes.

    Next, add a button to your form. Right click it and select Button Properties -> Edit Form Code.

    This will bring up the script editor. It will automatically start in the section for where your code will execute when the button is clicked.

    There will be the line //Write your code here. You can delete that line or just put your line of code underneath it(It's just a comment so it doesn't do anything).

    Just copy and paste that first line and it should work! Unless you need to change the Xpath first.

    Also, be careful when you copy and paste it becuase sometimes it will carry over some HTML tags from the webite as shown below.

    <p>XDocument.DOM.selectSingleNode("/my:myFields/my:field1").text = Math.round(Math.random()*10);</p>

  • 08-29-2011 11:44 PM In reply to

    • Joern
    • Top 150 Contributor
    • Joined on 09-28-2010
    • Posts 79

    Re: Random figure with button

    Thank you so much for your response.

    Unfortunately, I do not see the 'Copy Xpath'-possibility you advised me to select.

    Thus, I took your ("/my:myFields/my:field1") and simply translated 'field' into German, but then I receive following message: 'XDocument.DOM.selectSingleNode(...)' is null or no object.

    Any idea? Thanks for your patience :-)

  • 08-30-2011 02:19 AM In reply to

    Re: Random figure with button

    Hello Joern,

    ... a short info in german for better understanding ...

     "Es geht darum,. nicht nur 'field1' zu übersetzen, sondern es muss der Pfad zu Deinem Feld angegeben werden, den Du in der Datenquelle siehst, also, wenn der Pfad bspw. lautet "/my:meine Felder/my:Textfeld", dann muss EXAKT !!! dieser Pfad als Parameter bei selectsinglenode angegeben werden --- wenn´s diesen Pfad nicht gibt --- kommt die besagte Fehlermeldung..."

    The mentioned 'Copy XPath'-possibility is for IP 2007 or above only !!!

    Regards,

    Karsten
  • 08-30-2011 02:26 AM In reply to

    • Joern
    • Top 150 Contributor
    • Joined on 09-28-2010
    • Posts 79

    Re: Random figure with button

    Hello Karsten,

    thank you for translating.

    Well, in the meanwhile I assumed by myself, that the mentioned  'Copy XPath'-possibility is for IP 2007, since I did not find any button to copy it.

    Is there any possibility to see the XPath of a field in IP 2003?

  • 08-30-2011 02:34 AM In reply to

    • Joern
    • Top 150 Contributor
    • Joined on 09-28-2010
    • Posts 79

    Re: Random figure with button

    I got it.

    As Karsten said, I needed to translate not only "field1" into German but also "myFields".

    Now, there is another question arising:

    Is it possible to weight the probability of a random integer? For instance: In 40% of the cases I receive the integer 0 or 1, in 25% of the cases I receive the integer 2 or 3, and so on.

    0 to 1: probability of 40%

    2 to 3: probability of 25%

    4 to 5: probability of 15%

    6 to 7: probability of 10%

    8 to 9: probability of 8%

    10: probability of 2%

     

    Thank you very much in advance for your efforts.

  • 08-30-2011 06:36 AM In reply to

    • Joern
    • Top 150 Contributor
    • Joined on 09-28-2010
    • Posts 79

    In Excel I got what I want.

    Please find attached a screenshot for explanation:

    In Column C You find a couple of results.

    In Column B You find the corresponding cumulated probability of every result

    Cell G14 contains following formula: =VLOOKUP(RAND();B15:C52;2;TRUE)

    Is it possible to do this in code in IP 2003 so that the button click will generate a certain value considering the probabilities?

    Thank you very much in advance for your effort.

     


  • 08-30-2011 09:20 AM In reply to

    Re: Random figure with button

    This does the trick, just tested it out. You will just have to convert the Xpath values and you should be good to go.

     

    var temp = Math.random() * 10;

                var random = 0;

                if (temp >= 0 && temp <= 4)

                {

                    random = (Math.round(Math.random() * 10)) % 2;

                    XDocument.DOM.selectSingleNode("/my:myFields/my:field1").text = random;

                }

                else if (temp > 4 && temp <= 6.5)

                {

                    random = ((Math.round(Math.random() * 10)) % 2) + 2;

                    XDocument.DOM.selectSingleNode("/my:myFields/my:field1").text = random;

                }

                else if (temp > 6.5 && temp <= 8)

                {

                    random = ((Math.round(Math.random() * 10)) % 2) + 4;

                    XDocument.DOM.selectSingleNode("/my:myFields/my:field1").text = random;

                }

                else if (temp > 8 && temp <= 9)

                {

                    random = ((Math.round(Math.random() * 10)) % 2) + 6;

                    XDocument.DOM.selectSingleNode("/my:myFields/my:field1").text = random;

                }

                else if (temp > 9 && temp <= 9.8)

                {

                    random = ((Math.round(Math.random() * 10)) % 2) + 8;

                    XDocument.DOM.selectSingleNode("/my:myFields/my:field1").text = random;

                }

                else if (temp > 9.8 && temp <= 10)

                {

                    random = 10;

                    XDocument.DOM.selectSingleNode("/my:myFields/my:field1").text = random;

                }

  • 08-31-2011 01:37 AM In reply to

    • Joern
    • Top 150 Contributor
    • Joined on 09-28-2010
    • Posts 79

    Re: Random figure with button

    Thank you for your quick response.

    Still some questions:

    • Why do I have to write "&&" in the "else if..." row?
    • random = ((Math.round(Math.random() * 10)) % 2) + 4: What is the % 2 doing? Why do you add 2, 4, 6 and 8?

    Thanks for your patience.

     

  • 08-31-2011 07:00 AM In reply to

    • Joern
    • Top 150 Contributor
    • Joined on 09-28-2010
    • Posts 79

    Re: Random figure with button

    Hi Lockdown,

    one more thing, I have noticed:

    In the meanwhile I tried to modify what you have proposed.

    Here's what it looks like now:

    function CTRL2_5::OnClick(eventObj)

    {

    var temp = Math.random();           

    if (temp >= 0 && temp < 0.001)            

     {               

    XDocument.DOM.selectSingleNode("/my:meineFelder/my:feld1").text = 6;               

    XDocument.DOM.selectSingleNode("/my:meineFelder/my:feld2").text = 2;          

      }           

    else if (temp => 0.001 && temp < 0.003)

    {                

    XDocument.DOM.selectSingleNode("/my:meineFelder/my:feld1").text = 6;                

    XDocument.DOM.selectSingleNode("/my:meineFelder/my:feld2").text = 1;                        

    }           

    else if (temp => 0.003 && temp < 0.005)

     

    and so on....

     

    When I want to open the form I receive following message:

    Following failure occured:

    else if (temp =>0.001 && temp <0.003)

     

    What am I doing wrong?

  • 08-31-2011 08:53 AM In reply to

    Re: Random figure with button

    Q. Why do I have to write "&&" in the "else if..." row?

    A. This is the logical "AND" operator. It basically says, If x AND y AND z are not true, then run the code contained in the brackets beneath the if statement.

    Q. random = ((Math.round(Math.random() * 10)) % 2) + 4: What is the % 2 doing? Why do you add 2, 4, 6 and 8?

    A. The modulo operator (%) gives you a remainder from a division equation. So 5/2 = 2 with remainer of 1. That means 5%2 will be equal to 1. Doing the % 2 to the random number will give you either a 0 or a 1 since those are the only possible remainders when you divide something by 2. Now I added the 2, 4, 6, and 8 because you has that specific set of numbers you wanted. 40% weight on 0-1, 15% weight on 2-3, and so on. Adding 2 4 6 or 8 to the original 0 or 1 you get, will put you in those ranges. You can modify that as you wish and add whatever values and weights you want from it.

    You are getting an error because you have your equal sigh(=) and greater than signs (>) mixed up. Just switch those and you should be ok!

     

Page 1 of 1 (13 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.