I want learn how to make a javascript for an RPG where it generates and display a specific item, but the generation process is controlled by the percentage variable for items in certain category.
also, aside from the quality, and item type, it'd be nice if you could input a number in and it will also generate the item level from 3- to +3 (controlled by percentage) and display the number at the end modified by the aforementioned modifiers
These forums are generally for helping to troubleshoot problematic code, not for freebies.
__________________ ^_^
If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
* The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".
Here's a basic generator, but right now it's fully random, I want to control the rate of generation via probability.
like the probability of generating common is 60% or the probability of generating sword is 50%
one simple way is to simply build an array of the options, and repeat the items you want to be more common than others:
Code:
// helper function: random slot from array grabber:
// our dice-roll outcomes (some happen more often)
var rarity =[ "common", "common", "common", "uncommon", "rare"];
var weapons =[ "sword", "sword", "bow", "staff "];
// show a random pull using all the list's overlaped probabilities:
alert( rarity.random() );
now, all things being equal, a sword will hit 50% of the time, bow 25%, and staff 25%, common 60%, uncommon 20%, and rare 20%...
EDIT:
we can indeed calculate many-item probabilities using existing data and a little math. buckle your seatbelt:
Code:
Array.prototype.random=function random(){
function Rnd(w){return parseInt(Math.random()*(w+1));}
return this[Rnd(this.length-1)];
};
Array.prototype.counts=function(name){
var ob={};
this.forEach(function(a){ ob[a]=ob[a]?(ob[a]+1):(ob[a]=1); });
return name ? ob[name] : ob;
};
function combinedProb(rareness, weapon){
return (rarity.counts(rareness) / rarity.length) *
(weapons.counts(weapon) / weapons.length);
}
var rarity =[ "common", "common", "common", "uncommon", "rare"];
var weapons =[ "sword", "sword", "bow", "staff "];
combinedProb("common", "sword");//0.3
combinedProb("common", "bow");//0.15
combinedProb("rare", "bow");//0.05
in this fashion, we don't have to hand-code numbers or maintain tables of figures, we can just paste item names and let JS to the rest of the work.
edit2:
if you are using it a lot (hundreds of times or more) it might be worth it to pre-calc all the combined probabilities for instant lookup instead of on-demand calculation:
Code:
Array.prototype.counts=function(name){
var ob={};
this.forEach(function(a){ ob[a]=ob[a]?(ob[a]+1):(ob[a]=1); });
return name ? ob[name] : ob;
};
function combinedProb(rareness, weapon){
return (rarity.counts(rareness) / rarity.length) *
(weapons.counts(weapon) / weapons.length);
}
var rarity =[ "common", "common", "common", "uncommon", "rare"];
var weapons =[ "sword", "sword", "bow", "staff "];
var combined={}; //the table of combined probs
//populat prob table using all possible combos of rarity and weapons:
rarity.forEach(function(r){
var x=combined[r]={};
weapons.forEach(function(w){
x[w]=combinedProb(r,w);
});
});
//now, we can use the combined object to instantly lookup combos:
combined.common.sword //0.3
combined.common.bow //0.15
combined.rare.bow //0.05
this not only performs faster, it allows the developer to skip the extra quotes around the names, and avoids the parens, making it more letters than syntax, all in all: a lot less typing on a complex application.
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
But a big problem with this approach is the *granularity*.
Just to pick the example you used, if we wanted "rare" to be 1%, and still assuming the same 3 weapons (4 occurrences), we would need 20 values in the rarity array.
And *still* combined.rare.sword would be 2% instead of 1%.
And so on.
I think I would opt for a much simpler yet more flexible system:
But a big problem with this approach is the *granularity*.
Just to pick the example you used, if we wanted "rare" to be 1%, and still assuming the same 3 weapons (4 occurrences), we would need 20 values in the rarity array.
it doesn't matter how many values are in the array, so long as we don't have to code them like we would with some sort of table of numbers.
you can easily get to the single-percent level.
if you want 1% rare and the rest to be the same, the only piece you need to touch is the rarity array.
i'd imagine you want 75 common, 24 uncommon, and 1 rare:
And you really thing that is simpler and easier to understand than my table-based scheme?
Hmmm...maybe now I understand some of your coding better.
No, that was a joke. Your coding is top notch. But I do think that, in this case, your solution is too esoteric. Ehhh...one man's meat is another man's poisson distribution.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
And you really thing that is simpler and easier to understand than my table-based scheme?
.
it's not easier to understand, it's easier to maintain. in a table, adding a new class or changing a value almost guarantees that you'll need to edit several cells to "reCalc" the table.
with an iterative counter, you let the JS do the work.
i admit the tokens are a little simplistic for precise or complex calculations, but for many simple problems, it's a workable design pattern.
you can use an object instead of an array, you would have to sum all the values in the object, and then you have the same info as my item counter:
Code:
// util pure functions:
function sum(a,b){return a+b;}
function lut(key){return this[key];}
//object-based calculator:
function combinedProb(rareness, weapon){
return (rarity[rareness] / rarity.sum) *
(weapons[weapon] / weapons.sum);
}
//new data structure:
var rarity ={ common: 6, uncommon:2, rare:2 };
var weapons ={ sword:4, bow:2, staff:2 };
//pre-calc the whole to assist calcing the part each time:
rarity.sum=Object.keys(rarity).map(lut,rarity).reduce(sum);
weapons.sum=Object.keys(weapons).map(lut,weapons).reduce(sum);
//test it:
combinedProb("common", "staff"); //0.15
combinedProb("uncommon", "staff"); //0.05
combinedProb("common", "sword"); //0.3
__________________ my site (updated 5/13) STATS (2013/5) HTML5:90.2% MOB:15.2% IE7:0.5% IE8:8.4% IE9:8.5% IE10:8.5%
//new data structure:
var rarity ={ common: 6, uncommon:2, rare:2 };
var weapons ={ sword:4, bow:2, staff:2 };
It "reads" clearer (to me, at least!) and feels easier to make changes to. Actually, amazing how such a relatively minor rewrite helps the clarity so much.
But it still requires the user to understand the combinatorial math. Not at all difficult, but probably too much for a lot of the newbies we get here.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
A simple remark for @rnd me : Array(75).join("common,") build a string with 74 (and not 75 !) common, in the intervals of the 75 leer array's elements !
Last edited by 007julien; 01-27-2013 at 10:07 AM..