I'm assuming that javascript is interchangable between both versions of sugarcube here so I sincerely hope I am right x)
So I had been browsing the Sugarcube wiki for some time and still hadn't found too much relevant information to what I was looking for.
I was searching for a method to have 6 $object values that were completely randomised from a set amount from 0 - 150, but added up together would always add up to a certain amount say 600 or 150 (not both though but I havent decided on the final amount). I would also like to avoid decimals places in the concurrent values at all costs if possible.
I didn't have much luck on the twine databases I had found, but I did manage to find some javascript relating to this issue on a javascript website dubbed Stack Overflow. However, problems automatically arose as I could not find much in detail upon how to call JavaScript from the javascript section into an individual passage - whilst simultaneously using sugarcube/twine macros along with it.
I'm also wondering if this is the best method for this task, but I had no idea how to implement this with the twine/sugarcube macros (besides the random number generation really) so I was a little stuck.
Much obliged chaps
Here is the code in question but it might be easier just to avoid this in the first place, again - i'm not particularly sure whether this is covered in the macros or not.
var max = 36;
var r1 = randombetween(1, max-3);
var r2 = randombetween(1, max-2-r1);
var r3 = randombetween(1, max-1-r1-r2);
var r4 = max - r1 - r2 - r3;
function randombetween(min, max) {
return Math.floor(Math.random()*(max-min+1)+min);
}
function generate(max, thecount) {
var r = [];
var currsum = 0;
for(i=0; i<thecount-1; i++) {
r[i] = randombetween(1, max-(thecount-i-1)-currsum);
currsum += r[i];
}
r[thecount-1] = max - currsum;
return r;
}
Much obliged
P.S Apologies for the somewhat strenuous link between the title of this question and it's content, I was hoping this would also help in benefitting the community somewhat and hopefully avoid these sort of questions being asked in future x)
Thank you.
Comments
And you only need a little Javascript to implement the generate method from your example: ... I added a _upper and _rand to make the code slightly more readable.
I managed to implement that into my own version and give it a test run.
Unfortunately some of these values tend to revert to negative values, which unfortunately defeats the object of the system somewhat. I was also uncertain of how to implement an upper ceiling for the maximum amount any $r can equate to, but I wasn't sure where exactly to place the maximum value as each attempt was met with an error message.
I wasn't entirely sure of how this method worked in the first place because I had no means of testing it solely on a javascript level (currently), so I wasn't sure what the application of the second segment was.. Is it integral to this first part in any way? I honestly have no idea as on it's own it appears to do nothing:
And this is how it appears in passage when the two are together:
Is there anything that can be done to rectify this? Much appreciated.