×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    76

    Exclamation Math.random() does not work (QML)

    hi m8,

    i have a problem. i am developing a project. i am using a javascript file and i included it to a qml file. its turning a random number. but its turning same number everytime.

    function in example.js:
    Code:
    var sended = 0;
    
    function untitled(p1,p2) {
        sended = Math.floor(Math.random()*p1+p2);
        return sended;
    }
    thanx.
    best regards.

  2. #2
    Registered User
    Join Date
    Jul 2008
    Posts
    90

    Re: Math.random() does not work (QML)

    That might be so, but you have a very bad test case. Your variable sended will obviously have the return value of Math.floor(), not Math.random() and influenced by p1 and p2, whatever they might be. If you wish to check what Math.random() returns, test just for that and not anything else. _And_ if you ask here again, give some input values that you use that give the same results.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    76

    Re: Math.random() does not work (QML)

    thanx for ur reply @mvuori.

    i tried check it. i changed codes. "sended = Math.random()" but the results again same. everytime same. when i close the app, the result is changing for 1 time. and the results are same until the close. how can i fix it?

    best regards.

  4. #4
    Nokia Developer Expert
    Join Date
    Jun 2012
    Posts
    55

    Re: Math.random() does not work (QML)

    How are you using the sended variable after you generate the random number? Try outputting the variable to console using console.debug or console.log everytime you generate it and see if it still doesn't change.

    Br,
    Villep

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    76

    Re: Math.random() does not work (QML)

    @Villep thank u for ur reply.

    i am using button text for created number. "buttonn.text = sended" this line is in the javascript function. i saw the results are same when the function returns numbers.

    best regards.

  6. #6
    Nokia Developer Expert
    Join Date
    Jun 2012
    Posts
    55

    Re: Math.random() does not work (QML)

    @prodigy47
    What platform are you developing for? How and where are you calling the Javascript function that generates the random number? Is the number same for everytime you generate it if so, what is the generated number?

    Br,
    Villep

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    76

    Re: Math.random() does not work (QML)

    for example:

    Code:
    Image {
    anchors.rightMargin: Logic.untitled(30,30)
    anchors.leftMargin: Logic.untitled(50,30)
    }
    the result (returned variable) 10 for example (10 is first result). when the image again calls untitled function the result is again 10. the results are same from first result.

    i am a MeeGo developer.

    best regards.

  8. #8
    Nokia Developer Expert
    Join Date
    Jun 2012
    Posts
    55

    Re: Math.random() does not work (QML)

    Are you using the sended variable anywhere outside of the function? Can you try using

    Code:
    function randomNumber(from, to) {
       return Math.floor(Math.random() * (to - from + 1) + from);
    }
    You can use it like
    Code:
    Image {
    anchors.leftMargin: Logic.randomNumber(30, 50)
    }
    Also in your images rightmargin, what kind of value(s) were you looking for?

    Br,
    Villep

Similar Threads

  1. Math::IsNaN doesn't work?
    By ivansjg in forum Symbian
    Replies: 8
    Last Post: 2009-11-05, 08:48
  2. Math.random() issue on e71
    By funnydevil3000 in forum Symbian
    Replies: 3
    Last Post: 2009-10-07, 09:12
  3. javascript random NOT random
    By captain_dixie in forum Symbian
    Replies: 2
    Last Post: 2009-05-24, 02:33
  4. Math.exp() and Math.log() in MIDP 2.0
    By Larry101 in forum Mobile Java General
    Replies: 1
    Last Post: 2007-12-11, 08:50
  5. Math::Random()
    By chishti.hameed in forum Symbian
    Replies: 2
    Last Post: 2005-07-11, 08:39

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •