
Here's how you can use our tool to create and produce random number lists: Let precalculatedWeights = calculateCosineWeights(factor, intervals) įor(let i = runningIndex i < vals.Get the numerical results you want quickly with our random sequence generator. In my case, I get: [įunction calculateCosineWeights(factor, intervals)Ĭonst stepLength = 1 / (definitionSteps-1) The final output is printed to the console, and it looks very similar to your mockup graph in your question. I did it this way for performance to simulate 10,000 runs, but using the weights array, you can directly map any random number between to its corresponding weight.
#RANDOM NUMBER GENERATION CODE#
The last part of the code just calculates 10,000 random numbers, sorts them, and maps them to their respective indexes.
#RANDOM NUMBER GENERATION PLUS#
We will then cumulatively sum each weight so that each value in our weights array is equal to itself, plus the sum of all the preceding weights. The domain of our function we defined above is, so we will divide that into 13 equal steps to calculate the probability of each number. In the case of your example, that is 0-12, or 13 possible values.
/GettyImages-627412588-5be9cb95c9e77c0026b8fa3c.jpg)

Math.random() always returns a number between 0 and 1, and we want to map that value to the range of possible values. Now that we have this function, we can determine how likely any number is to appear. That function translated to JS code looks like this: const func = (x) => (1 + ((factor - 1)*(1 - s(2*Math.PI*intervals*x))/2))/(factor) This function allows me to enter any x to get its relative probability. In this example, I used your factor of 5 and intervals of 3 to produce this graph. Where a represents intervals and b represents factor. We also want that function to start and end at the minimum value and reach the maximum value a certain number of times. We want a cosine function with a given amplitude between the most likely numbers and the least likely numbers. This code is in javascript, both because I'm more familiar with it than ruby and because it can then be embedded on S.O., but the principles are the same and you should be able to translate it.

This sounded like a fun problem, so I gave it a go.

# more likely high numbers when the cos is nearer to 1 # Trying to use the `num` from above to then multiply by the factor to give us # Grab a random index and apply it to the squished cos Here is an example of my most recent iteration, although it just has a descending chart: def randcos(factor:, intervals:) I can get it to show the one "spike" just using Math.sin/s, but I want to be able to customize how many of them show up. I've been trying to use s to do this, but I'm getting stuck when trying to map those higher numbers to a likelihood of generating a new set. The low points still occur, but are approximately 1/5 as likely because we set factor: 5. (num * 13).floor # Group in 12 groups for graphing visibilityĪnd then graphing those points based on frequency, I would want a chart that looks something like this:īecause we set intervals: 3, we have 3 "spikes" where those groups of numbers are more likely. The goal is to generate a number between 0 and 1, but be able to supply an amplitude/factor and a period/interval.įor example, given the following: 10_ do I am trying to generate a number that follows a cosine curve with an increased period.
