| Off Topic > Off Topic |
| Programming Megathread |
| << < (83/241) > >> |
| Foxscotch:
ok, so... I'm trying to make a function (in python, but will be converted to js later) that picks a "random" number, but it needs to be weighted the arguments should be a minimum, maximum, number to be biased towards, and the magnitude with which it should be biased (that still needs some defining) I've found a lot of examples but almost all of them use squares or square roots which force you to bias towards the top or bottom, and I want it to be variable and now I have two ideas, mostly inspired by what other people have said: create a list of numbers from the min to the max, then go back through it and add more numbers, depending on the bias arguments like, with min=1 and max=5, biased towards 3, the end result list might be [1, 2, 2, 3, 3, 3, 4, 4, 5] or the same min and max biased towards 1 might be [1, 1, 1, 2, 2, 3, 4, 5] see what I mean? then I pick a random item from that list. the magnitude argument would determine how many of each number is added also not sure how to determine how quickly the number of numbers added should drop off as it gets further from the number it should be biased towards. reducing by one for each number seems too slow I thought I had another idea but actually no I don't. plz help |
| Headcrab Zombie:
--- Quote from: Foxscotch on January 29, 2016, 11:53:55 AM ---number to be biased towards, and the magnitude with which it should be biased --- End quote --- Based off these descriptions, it sounds like your looking for a normal distribution. In which case, the two terms you're describing would be 'mean' and 'standard deviation' A normal distribution doesn't have any actual upper and lower limit, though, just an increasing improbability as you get further from the mean. So you could combine it with a clamp method if you need absolute insurance of a range. I don't have any code for you, but knowing the name of what you're looking for should help immensely Edit: http://rosettacode.org/wiki/Statistics/Normal_distribution#Python |
| Foxscotch:
mmm thanks that code example for python is unimportant for the most part, it just shows how to graph the results of the standard library function random.gauss() which is what I'm actually looking for here. that function. javascript however does not seem to include such a function, but thankfully random.guass() is mostly pure python so I may be able to convert it to js myself but knowing what I'm looking for does help a lot, ty ily edit: I wish I was a math whiz. this kinda stuff is like the only reasons I have for wanting to go to college. feels like it'd be a waste to go through all of that just for the computer science and math classes though....... double edit: successfully used some code from random.gauss() to make the function in python: def roll(min_v=1, max_v=100, mean=None, sigma=None): if mean is not None and sigma is not None: x2pi = random.random() * (2.0 * math.pi) g2rad = math.sqrt(-2.0 * math.log(1.0 - random.random())) z = math.cos(x2pi) * g2rad result = mean + z*sigma if result < min_v: return min_v elif result > max_v: return max_v else: return int(round(result)) else: return random.randint(min_v, max_v) next up is turning that into javascript (the min and max arguments have "_v" added because "_value" was too long, and min() and max() are built-in python functions which I didn't want to mess with) one glaring issue is that if mean is near or equal to either the min or the max, that min or max number becomes probably significantly more likely to occur. I guess that is probably impossible to prevent? maybe? at least in any way that isn't convoluted, unless there are more crazy math tricks I don't know about triple edit: that was pretty easy function randint(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function roll(min, max, mean, sigma) { if (typeof mean != 'undefined' && typeof sigma != 'undefined') { var x2pi = Math.random() * (2.0 * Math.PI); var g2rad = Math.sqrt(-2.0 * Math.log(1.0 - Math.random())); var z = Math.cos(x2pi) * g2rad; var result = mean + z*sigma; if (result < min) return min; else if (result > max) return max; else return Number(Math.round(result)); } else { return randint(min, max); } } overedit: how can I make eclipse look better............. it has a "dark" theme which is mostly good except all of the issues. the editor window looks great but in other menus there are a lot of occasional problems with dark-colored text on dark backgrounds. the close buttons on the tabs also look very odd using the lighter themes is ok but not preferable |
| ZSNO:
For part of a project in one of my classes, I had to create a assembly program to multiply two numbers using only "add", "nor", and "beq". (And no just adding a number to itself x times, way too inefficient) Was pretty interesting. |
| Nickelob Ultra:
Anyone have any books they recommend for C and/or C#? Preferably on Amazon kindle unlimited but open to suggestions, looking to read moar. |
| Navigation |
| Message Index |
| Next page |
| Previous page |