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.
[B]function in example.js:[/B]
[CODE]var sended = 0;
function untitled(p1,p2) {
sended = Math.floor(Math.random()*p1+p2);
return sended;
}[/CODE]
thanx.
best regards.
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.
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.
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
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.
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
Re: Math.random() does not work (QML)
for example:
[CODE]Image {
anchors.rightMargin: Logic.untitled(30,30)
anchors.leftMargin: Logic.untitled(50,30)
}[/CODE]
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.
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);
}[/CODE]
You can use it like [CODE]Image {
anchors.leftMargin: Logic.randomNumber(30, 50)
}[/CODE]
Also in your images rightmargin, what kind of value(s) were you looking for?
Br,
Villep