Help in random number generating

74 views
Skip to first unread message

Turbo Reemix

unread,
Oct 19, 2016, 12:59:15 PM10/19/16
to DroidScript
I'm creating a random number generator, but it don't follow the min and max to generate the random number, sorry for bad english
function Roll(min, max){
   
return Math.round(Math.floor(Math.random() * (max - min + 1)) + min);
}
function OnStart(){
    app
.LoadPlugin("UIExtras");
    uix
= app.CreateUIExtras();
    app
.EnableBackKey(false);
    thm
= app.CreateTheme("Dark");
    thm
.SetBackColor("#ff303030");
   
    home
= app.CreateLayout("linear","VCenter,FillXY");
    lTop
= app.CreateLayout("linear","horizontal");
   
TopTxtContainer = app.CreateLayout("linear","horizontal,left");
    ttl
= app.GetAppName();
    ttl
= app.CreateText(ttl,0.8, 0.085,"Left,AutoScale");
    ttl
.SetTextSize(48);
    ttl
.SetMargins(0,0,0,0);
    ttl
.SetPadding(0,0,0,0);
    ttl
.SetFontFile("assets/Roboto.ttf");
   
TopTxtContainer.AddChild(ttl);
    lTop
.AddChild(TopTxtContainer);
   
    home
.AddChild(lTop);
   
    trow
= app.CreateLayout("linear","VCenter");
    trow
.SetSize(1.0,0.915);
   
    res
= app.CreateText("", 0.3, 0.3, "Bold,AutoScale,Top");
    res
.SetTextSize(96);
    trow
.AddChild(res);
   
    layBtn
= app.CreateLayout("linear", "horizontal");
   
   
def = ["D4","D6","D10","D12","D20"];
   
for(i=0;i<5;i++){
        btn
= app.CreateButton(def[i],0.15,0.1);
        btn
.SetOnTouch(calc);
        layBtn
.AddChild(btn);
   
}
    trow
.AddChild(layBtn);
   
    home
.AddChild(trow);
    app
.AddLayout(home);
   
    app
.SetTheme(thm);
}
function calc(){
   
switch(btn.GetText()){
       
case "D4":
            chr
= Roll(1,4)
           
if(chr>4){chr--}
            res
.SetText(chr);
       
break;
       
case "D6":
            chr
= Roll(1,6)
           
if(chr>6){chr--}
            res
.SetText(chr);
       
break;
       
case "D10":
            chr
= Roll(1,10)
           
if(chr>10){chr--}
            res
.SetText(chr);
       
break;
       
case "D12":
            chr
= Roll(1,12)
           
if(chr>12){chr--}
            res
.SetText(chr);
       
break;
       
case "D20":
            chr
= Roll(1,20)
           
if(chr>20){chr--}
            res
.SetText(chr);
       
break;
   
}
}Digite o código aqui...


Symbroson Development

unread,
Oct 19, 2016, 1:31:39 PM10/19/16
to DroidScript
var num = random(1,6)

function random(min,max) {
return Math.random()*(max-min)+min
}

Turbo Reemix

unread,
Oct 19, 2016, 1:42:09 PM10/19/16
to DroidScript
Thanks but this don't is the problem
I have stuck in getting who is the max
Message has been deleted

Netpower8

unread,
Oct 19, 2016, 1:50:16 PM10/19/16
to DroidScript
this might help you... just adjust the code for you need... this is part of my plugin library... part or parts of the code came from this forum also....

// ******************************
// * random tenth
// ******************************
// generate random number with 1 decimal point
function getRandomTenth(min,max) {
    app.SetDebugEnabled(false); // disable debug
    var a=max*10;
    var b=(min-1)*10;
    var c=Math.random()*(a-b)+b;
    var ret=Math.floor(c)/10;
    app.SetDebugEnabled(true); // enable debug
    return ret;
} // getRandomTenth(min,max)

Symbroson Development

unread,
Oct 19, 2016, 1:59:35 PM10/19/16
to DroidScript
Then you could also use .toFixed(n) to get n decimals:

function random(min,max) {
    return (Math.random()*(max-min)+min).toFixed(2)
}

luke x3

unread,
Oct 21, 2016, 3:47:28 AM10/21/16
to DroidScript
Few ideas

btn[i] = CreateButton(...);
btn[i].myvalX = 12;

switch(this.GetText()){


var x = this.myvalX;

chr = Roll(1, x);
Reply all
Reply to author
Forward
0 new messages