All Questions

Tagged with
Filter by
Sorted by
Tagged with
30 votes
7 answers
35k views

Generate random integers with probabilities

I'm a bit confused about how to generate integer values with probabilities. As an example, I have four integers with their probability values: 1|0.4, 2|0.3, 3|0.2, 4|0.1 How can I generate these ...
Headshota's user avatar
  • 21.3k
30 votes
4 answers
9k views

How to generate random numbers biased towards one value in a range?

Say, if I wanted to generate an unbiased random number between min and max, I'd do: var rand = function(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; }; But what if I ...
c00000fd's user avatar
  • 21.7k
11 votes
3 answers
14k views

Probability of getting the same value using Math.random

The requirement is to send a unique id to database when user click on submit button. So I am using Javascript Math.random method. I just want to know what are the chances or possibility to get same ...
Jitender's user avatar
  • 7,761
8 votes
1 answer
122 views

Function that returns array of array combinations

I'm trying to make a _.combinations function (underscore mixin) that takes three parameters arr, pockets, duplicates. Here's a test that I designed to show how the behavior should be. expect(_....
ThomasReggi's user avatar
  • 57.2k
7 votes
10 answers
11k views

JavaScript - How to randomly sample items without replacement?

JavaScript I've tried searching for something like this, but I am not able to find it. It's a simple idea: a. Take a random number between 0 to 10. b. Let's say the random number rolled is a 3. c....
seRgiOOOOOO's user avatar
6 votes
1 answer
7k views

std normal cdf, normal cdf, or error function

Can any of these functions, the standard normal cumulative distribution function, the normal cumulative distribution function, or the error function, be reliably calculated with javascript? I'd like ...
user avatar
6 votes
2 answers
3k views

Get random item for array based on different probabilities?

Okay, imagine I'm creating a Pokemon game in JavaScript. I have an object like this... pokemon = { "pikachu": {hp: 100, probability: 0.1}, "squirtle": {hp: 90, probability: 0.2} }; I basically ...
Danny's user avatar
  • 993
5 votes
2 answers
686 views

How to increase or decrease the probability of an item of an array being picked? [duplicate]

So, let's say I'm making something like a slots machine, to use the emojis I'd like to use I'd define them in an array. var arr = ["emoji","emoji2","emoji3","emoji4","emoji5"] Let's say I'd want ...
SomePerson's user avatar
  • 1,259
4 votes
1 answer
1k views

Can you return n choose k combinations in Javascript using Array.flatMap?

For instance, this is 5 choose 2: var array = [0,1,2,3,4]; var result = array.flatMap( (v, i) => array.slice(i+1).map(w => [v, w]) ); console.log(result); ...
Yuval Levental's user avatar
4 votes
2 answers
251 views

How to calculate what is probability of getting same result 8 times in a row, when flipping coin 1000 times?

I've tried to use this code: function calc (n, c) { let a = 0 const omega = Math.pow(2, n) let search1 = '' let search2 = '' for (let i = 0; i < c; i++) { search1 += '0' } for (...
Gieted's user avatar
  • 845
4 votes
2 answers
3k views

Different probability for ranges of random numbers

I'm looking for the best way of implementing random number generator, that will allow me to have control over probability from what range the generated number will be returned. To visualize what I'm ...
sasklacz's user avatar
  • 3,618
4 votes
2 answers
116 views

JS inverting probabilities

I'm generating a grid of objects, each of which has one of 3 colors. Assume I'm filling a particular grid cell. I know that around this cell there are, for example, 1 object of color 0, 1 of color 1 ...
Alex Chashin's user avatar
  • 3,395
4 votes
1 answer
93 views

How "best" to allow a web-page visitor to construct mathematical or statistical tools?

I have a free web site that streams real-time stock-options data. I want to let users make and then save their own JavaScript-callable tools to interpret options data. Users can invoke these custom ...
Pete Wilson's user avatar
  • 8,688
3 votes
2 answers
13k views

Won't Math.floor(Math.random() * 255) generate uneven probabilities?

Was thinking through the logic and it seemed that Math.floor(Math.random() * 255) would generate uneven probabilities of each value being generated. For example, a number of values can round down to ...
Kyle Chadha's user avatar
  • 3,941
3 votes
1 answer
122 views

How to guarantee a certain number of random occurrences in a loop?

I have a program that simulates the rotation of a clock's hand and it randomly jumps 2x the normal rotation amount. I need to guarantee that in a full rotation, I get at least n jumps. How should I go ...
Ayman's user avatar
  • 31
3 votes
1 answer
649 views

An efficient version/alternative to the alias method that samples without replacement

I am writing a quiz/teaching game in HTML5/JS in which the player is offered a set of 10 questions from a larger mastery set. The game keeps track of the players' scores over time and is more likely ...
TheHans255's user avatar
  • 2,117
3 votes
1 answer
2k views

Collision rate of two 32-bit hashes vs one 64-bit hash? (uncorrelated?)

I have seen a couple questions that ask "do two 16-bit hashes have the same collision rate as a 32-bit hash?" or "do two 32-bit hashes have the same collision rate as a 64-bit hash?" And it seems like ...
bryc's user avatar
  • 13.7k
2 votes
2 answers
170 views

Find item in array using weighed probability and a value

Last week I had some problems with a simple program I am doing and somebody here helped me. Now I have run into another problem. I currently have this code: var findItem = function(desiredItem) { ...
Penn-'s user avatar
  • 136
2 votes
1 answer
622 views

Return value based on probability and percentage

Any idea how I could implement probability in regards to these examples. let directions = [ { 1: { left: { successRate: 1, direction: 0 ...
Jordan Brown's user avatar
2 votes
1 answer
148 views

What kind of visualization should I use for displaying combinations?

I need to create a visualization/chart showing all the ways to choose from a set of items (i.e., number of possible combinations) Concretely, I am showing potential offspring from two animals, ...
John Lehmann's user avatar
  • 8,085
1 vote
2 answers
4k views

How do I convert probability into z-score

Javascript> If you are in the data science industry, you would be bothered if you don't have normal distribution table. I came across the article in Stackoverflow that converts z-score to probability ...
Gregory's user avatar
  • 111
1 vote
1 answer
565 views

Creating an algorithm to define matching probability of strings in Javascript

I am working on google map and trying to find specified place. But, sometimes I get more results of specified place (as I am trying to search the place with its name in whole city). So I have limited ...
sagar's user avatar
  • 732
1 vote
3 answers
776 views

Javascript function to generate random integers with nonuniform probabilities

In javascript (or jquery) is there a simple function to have four integers with their probability values: 1|0.41, 2|0.29, 3|0.25, 4|0.05 how can I generate these four numbers taking into account ...
shelbypereira's user avatar
1 vote
1 answer
130 views

What is a Google Apps Script to Calculate the Probability of Independent Events?

I am trying to create a script that calculates the probability of 0-20 wins in a 20-game season, with the results going in Column F of this spreadsheet. Finding the probability of 0 and 20 wins is ...
Dustin's user avatar
  • 59
1 vote
2 answers
546 views

How do I assign a probability (uniform increasing or decreasing linear distribution) to a array of values?

Given X=[1,2,3,4,5,6,7,8,9,10] -- but X could be any length(N). I want to achieve the following: I want to give the 1st value X[0], the highest probability. I want to give the 2nd ...
jasan's user avatar
  • 12.1k
1 vote
1 answer
777 views

Generate a random number with a non-uniform distribution

I have a formula that generates a random integer in a given range [x,y]. rand = Math.floor(x + Math.random()*(y-x+1)); And I would like the generated integer to have a higher chance of being close ...
CrackerKraken's user avatar
1 vote
2 answers
2k views

Weighted probability random choice array

I have an array and returning random values. const array = [ 1, 2 ,3 ,4 ,5, 6, 7, 8] const rand = array[~~(Math.random() * array.length)] I would like to return a random element of the array, but ...
fitzmode's user avatar
  • 1,016
1 vote
1 answer
161 views

Get content from arrays with declared probability in JS

i want to draw values from two or more arrays. For example arrays 'age' which contain values 1-99 and second array 'earnings' which contain 2000-10000. If i will draw 'age' more than 30 i would like ...
Dawid Kwiatoń's user avatar
1 vote
1 answer
4k views

Spin wheel result to be already controlled

I'm using this code to create a spinning wheel. But I want to control the result. For example the prize $10000 I want it with less percentage. So, not anyone can get maybe 1 in 500 people. I didn't ...
Ghezlan AL-Nofly's user avatar
1 vote
0 answers
76 views

How to populate array with numbers based on odds?

I recently saw a roulette wheel of sorts which contained the following possible numbers. 1, 2, 9, 16, 24, 49j and 49f. Each number has odds of itself over 1. So the odds of rolling a 2 are 2/1 and a 9 ...
Maelstrom's user avatar
1 vote
0 answers
319 views

How to create a javascript function that calculates probability percentages in values from 0-99

in the following code, pretend the two broken images are each dice that show anything from 0-9. that means that when you add their values they could show anything from 0-99 making them 100 sided. In ...
ImNewToStackOverflow's user avatar
0 votes
2 answers
3k views

JavaScript: How To Code a Heads/Tails With Specific Probability Chance Percentage?

I've implemented the function: function coinFlip() { return(Math.floor(Math.random()*2) === 0) ? 'Heads' : 'Tails'; } And it's all working fine (I already tested it). My problem is, how do ...
5120bee's user avatar
  • 709
0 votes
2 answers
143 views

How to randomly return associative array names based on its values

I've a set of campaigns that correspond to a number of "points." Looks something like this: [ {"c1":4, "c2":8, "c3":25} ] I want to randomly pick a campaign from this set. I'm assuming rand() will ...
dsp_099's user avatar
  • 5,910
0 votes
1 answer
530 views

Why is my permutation calculator returning decimals?

I made a permutation and combination calculator. The code to find the factorial of a number is: function factorial(num){ total=1; if(num==1||num==0){ return total; } else{ for(i=2;i<=num;...
shurup's user avatar
  • 821
0 votes
3 answers
73 views

Why does the score not equal 0 when I roll a 1?

So I'm making a dice game where if either ofd the dice roll a 1, the score is 1, but I can't seem to make that work. I believe everything else it ok. var die1 = Math.floor(Math.random()*6 + 1); ...
JackWalsh's user avatar
0 votes
3 answers
705 views

How to control the probablity of an event given a certain number of dice rolls

I have a turn based game. I set a condition on the player that for the next X fixed number of turns he/she has a Y percentage chance of triggering some other event (say game over). So say x = 5 ...
Neil Collier's user avatar
0 votes
1 answer
67 views

Probability Function over a fixed number of instances

I need to build a Boolean probability test that can be run periodically with increasing chances of returning true. The increase in probability of returning a positive test is to be determined by ...
Mark Taylor's user avatar
  • 1,128
0 votes
2 answers
1k views

How to set win occurrence or percentage in JavaScript

I have a number game (lotto) where there's an array of 1 - 36. myNum = [1,2,3...36]; The player is to choose 6 numbers from 1 - 36 (into an array). I generate random 6 numbers too (into another ...
Olawale Oladiran's user avatar
0 votes
1 answer
50 views

Drawing with propalibity [duplicate]

I have object in js like this: const Obj = [ { propability: 0.5, name: 'Item with propability 0.5%' }, { propability: 1, name: 'Item with propability 1%' } ]; Any ...
sarakosek's user avatar
0 votes
1 answer
80 views

Generate Random String with Dependent Probability

Using JavaScript or jQuery, I'm trying to create a string generator that relies on dependent probability. For example, if I generated "doctor" on the first go, no other "doctors" would appear. It ...
aman.s's user avatar
  • 86
0 votes
1 answer
138 views

How to generate a random distribution to make characters (in a word/phrase) appear more naturally

This is the effect I am trying to achieve: http://codepen.io/anon/pen/ENzQem I am trying to generate an effect where the letters of a string get revealed gradually and randomly. See the codepen link ...
adrianmcli's user avatar
  • 1,976
0 votes
4 answers
693 views

Loop through array of items using probability [duplicate]

I am pretty new to JavaScript and I have been making while-loops that rolls a dice and breaks out of the loop when you roll a 6. var rollDice = function() { var dice = Math.floor(Math.random() * 6 +...
Penn's user avatar
  • 13
0 votes
1 answer
470 views

How do I calculate the probability of a presidential candidate winning?

I have an object like const data = { 'Washington' : { ElectoralVotes : 12, RChance: 0 }, 'Oregon': { ElectoralVotes: 7, RChance: 15 }, . . . 'Hawaii' : { ElectoralVotes: 4, ...
user6048670's user avatar
  • 2,891
0 votes
1 answer
481 views

An algorithm for multiplying and marginalizing probability tables

I am working implementing a specific Bayesian network library in Javascript. Since I couldn't find any other library available online, I had to start from the scratch, including multiplying and ...
jhonatanoliveira's user avatar
0 votes
2 answers
155 views

Translate mathematic formula to Javascript

I have this formula: f(x)=(x^0+x^0+x^1+x^101+x^2+x^202)^n which is basically probabilities when rolling n dice with faces showing values: {0,0,1,101,2,202} How do I translate that to JavaScript? ...
Wildhorn's user avatar
  • 936
0 votes
1 answer
43 views

how to don't have the same bg 2 times in a row in this random background js

I've a problem with the same bg js as the last question; this is the js : ChangeIt(); function ChangeIt() { var totalCount = 12; var num = Math.ceil( Math.random() * totalCount ); document.body....
Andrea Cazzola's user avatar
0 votes
2 answers
90 views

Another dice error, code not quite working

Line 6 says it's expecting an identifier. I'm trying to make it so rolling a 1 on either die makes the score 0, and rolling doubles doubles your score, otherwise, the score is the sum of the two die. ...
JackWalsh's user avatar
0 votes
1 answer
239 views

distributional sampling in Node.js

I wonder how it's possible to perform distributional sampling in Node.js. In particular, I have the number of elements, where the i'th value of the arrays is the probability of the element i'th, like ...
user16168's user avatar
  • 625
0 votes
1 answer
734 views

Item generator controlled by probability

I'm pretty new to JS but I want to make a very basic RPG item generator that is controlled by probability. This is what I have come up with. I have a problem with the itemtype part. After the ...
user2002516's user avatar
0 votes
1 answer
134 views

Generating a random number within a large range based on probabilities

I am curious how Stake.com managed to create the game "Limbo" where the odds of a multiplier happening is specific to the probability they've calculated. Here's the game : https://stake.com/...
rt10's user avatar
  • 35