Generate a random boolean 70% True, 30% false - javascript

I have the following function that generates a random boolean.
choose_direction: function () {
var random_boolean = Math.random() >= 0.5;
if (random_boolean) {
trade.call()
prev_trade.goingUp = true
console.log('Trade: CALL!!!!!!!!!!!!!!!!!!!!!!')
} else {
trade.put()
prev_trade.goingUp = false
console.log('Trade: PUT!!!!!!!!!!!!!!!!!!!!!!!!')
}
}
However, I need the distribution to be unfair. More specifically, I want the output to be 70% of the time true and 30% of the time false.

Instead of >= 0.5 you just need < 0.7:
var random_boolean = Math.random() < 0.7;
// 70% this will be true, 30% false
As #plasmacel commented, Math.random() returns a value between 0 and 1 (including 0, but not 1: [0, 1)), so therefore we do < 0.7.

Let's consider one thing. Math.random() gives you a pseudo-random number.
Try this function:
function randomTest(triesCount) {
var sum = 0;
for(var i = 0; i < triesCount; i++){
sum += Math.random();
}
return sum / triesCount;
}
if you try it with different triesCount parameters the results will be:
randomTest(100) - 0.5189474703446081
randomTest(100) - 0.5189474703446081
randomTest(1000) - 0.4973368682657417
randomTest(10000) - 0.5001058467610172
randomTest(100000) - 0.4987280984186288
randomTest(1000000) - 0.4999987387801045
randomTest(10000000) - 0.49998292815655454
randomTest(100000000) - 0.500079160302315
So as you can se the results go to 0.5 which means this is not a random number generated here.
Although the answer to your question will be
var random_boolean = Math.random() >= 0.3;
as Ionică Bizău said.
But keep in mind that it is pseudo-random number!

Here is a version I made with typescript. Can easily remove the variable types to turn this into javascript.
//Usage
let randomBoolean = randomPercentForTrue(70)
export function randomPercentForTrue(percentage: number): boolean {
return randomNumber(1, 100) <= percentage
}
export function randomNumber(minValue: number, maxValue: number): number {
return Math.floor(Math.random() * (maxValue - minValue + 1)) + minValue
}
These methods are heavily tested with various use cases. Here are test cases with mocha:
it('randomPercentForTrue with 95% accuracy', function() {
let results0: boolean[] = []
let results1: boolean[] = []
let results2: boolean[] = []
let results3: boolean[] = []
let results4: boolean[] = []
let loopAmount = 10000
for(let i = 0; i < loopAmount; i++) {
results0.push(randomPercentForTrue(0))
results1.push(randomPercentForTrue(25))
results2.push(randomPercentForTrue(50))
results3.push(randomPercentForTrue(75))
results4.push(randomPercentForTrue(100))
}
let resultTotals = Array(5).fill(0)
for(let i = 0; i < loopAmount; i++) {
resultTotals[0] += results0[i]
resultTotals[1] += results1[i]
resultTotals[2] += results2[i]
resultTotals[3] += results3[i]
resultTotals[4] += results4[i]
}
expect(resultTotals[0]).to.be.closeTo(0, 0)
expect(resultTotals[1]).to.be.closeTo(loopAmount * 25 / 100, loopAmount * 25 / 100 / 20)
expect(resultTotals[2]).to.be.closeTo(loopAmount * 50 / 100, loopAmount * 50 / 100 / 20)
expect(resultTotals[3]).to.be.closeTo(loopAmount * 75 / 100, loopAmount * 75 / 100 / 20)
expect(resultTotals[4]).to.be.closeTo(loopAmount, 0)
})

Related

How to check if a variable value is increasing or decreasing and show the number with what it increased or decreased

This is a dumb question, And I can't believe I asked for a solution, Well now that I am pretty good I answered it. So, Firstly I create the variable number then I add two properties to it's prototype called oldValue and randomize then I set randomize to a random decimal number between 0 and 1 using Math.random(), I store the value of the number variable before any further changes, In the property I added to the prototype called oldValue, After that I check if the randomize is less than 0.5 or more than 0.5 if less then I decrement a random number between 50-100 else I increment with a number between 50-100, At last I console.log() the changes.
let number = 0;
number.__proto__.oldValue = number;
number.__proto__.randomize = 0;
let interval = setInterval(() => {
number.__proto__.randomize = Math.random();
let randomNumber = Math.floor(Math.random() * (100 - 50) + 50);
number.__proto__.oldValue = number;
if (number.randomize > 0.5) number += randomNumber;
else number -= randomNumber;
console.log((number - number.oldValue) < 0 ? `decrement: ${number} ${number.oldValue}` : `increment: ${number} ${number.oldValue}`);
}, 100)
You can store the delta in a variable. Additionally, to generate a random boolean, you only need to check if Math.random() is greater than 0.5.
var number = 0;
setInterval(() => {
let delta = Math.floor(Math.random() * 100) * (Math.random() > .5 ? 1 : -1);
console.log('Delta:', delta);
number += delta;
}, 100)
When you are creating random number you can store it. Math.floor(Math.random() * 100)
Like this:
var number = 0;
var randomize = 0;
setInterval(() => {
randomize = 0;
randomize += Math.floor(Math.random() * 100);
if (randomize > 50) {
let newRandomise = Math.floor(Math.random() * 100);
number += newRandomise
console.log("increased by", newRandomise)
}
else {
console.log("deccreased by", randomize)
number -= Math.floor(Math.random() * 100)
}
}, 100)
Just add some console.log s that will help you
var number = 0;
var randomize = 0;
setInterval(() => {
randomize = 0;
randomize += Math.floor(Math.random() * 100);
if (randomize > 50) {
const incrementer = Math.floor(Math.random() * 100);
console.log('Increased by', incrementer);
number += incrementer;
}
else {
const decrementer = Math.floor(Math.random() * 100);
console.log('Decreased by', decrementer);
number -= decrementer;
}
}, 100)

find minimum numbers of currency for required amount

I made a little program to find minimum number of notes(currency) for the required amount. for example, let's say I enter an amount 1121 and I have an array of these values below:
notes = [1, 2, 5, 10, 50, 100, 200, 500] so my final result will be:
500 * 2(notes) = 1000
100 * 1 = 100
20 * 1 = 20
1 * 1 = 1
then the total would be 1121. any help with understanding would be much appreciated. I know it only takes one for loop but I'm confused in some parts.
here's what I did: https://stackblitz.com/edit/angular-rufwzk?file=src%2Fapp%2Fapp.component.ts
Here is the corrected code of what you want. I hope i get it right.
requiredNotes(amount) {
let noteArry = this.notes,
quotient,
remainder,
temp,
noteCount = 0,
eachNote,
remainingAmount = 0;
for (let i = noteArry.length - 1; i >= 0; i--) {
if (amount >= noteArry[i]) {
quotient = Math.floor(amount / noteArry[i]);
remainder = amount % noteArry[i];
amount = amount - (noteArry[i] * quotient);
if (amount == 0) {
console.log("note:-", noteArry[i], ",number of note", quotient);
break;
} else if (amount != 0) {
console.log("note:-", noteArry[i], ",number of note", quotient);
}
} else {
continue;
}
}
}
I simplified a little your code (using JS Map):
...
notesMap = new Map();
...
requiredNotes(amount) {
for (let i = this.notes.length - 1; i >= 0 && amount; i--) {
const qty = Math.floor(amount / this.notes[i]);
qty && this.notesMap.set(this.notes[i], qty);
amount = amount % this.notes[i];
}
const entries = Array.from(this.notesMap.entries());
this.requireNotes = entries.map(([curr, qty]) => `${curr} * ${qty} = ${curr * qty}`);
}
STACKBLITZ
for (let i = noteArry.length - 1; i >= 0; i--) {
if (amount >= noteArry[i]) {
quotient = Math.floor(amount / noteArry[i]);
remainder = amount % noteArry[i];
remainingAmount = noteArry[i] * quotient;
amount=amount-remainingAmount;
console.log('number of notes =', noteArry[i], 'x', quotient,' notes');
}
}
This does the trick simply logs out the number of notes for the mentioned amount.

Javascript Loop with adding same value

I hope you guys can help me!
I'm trying the following:
1000 + 100 = 1100 * 2 = 2200;
2200 + 100 = 2300 * 2 = 4600;
4600 + 100 = 4700 * 2 = 9400;
9400 + 100 = 9500 * 2 = 19000;
...
But I'm getting the flowing result.
1000 + 100 = 1100 * 2 = 2200;
2200 * 2 = 4400
4400 * 2 = 8800
...
The values are inputed by the user, but I used static values for the example.
I have te following code:
var generate = function generate(rate){
var array = [];
s=0;
for (var i = 0; i < 10; i++){
s =+ 100;
array.push([
(parseInt(1000) + 100) * Math.pow(2, i),
]);
}
return array;
}
Sounds to me like you want a function that adds 100, doubles and then calls itself recursively a fixed number of times.
This function will run 10 times and output the final answer:
function add100andDouble(num, runTimes){
if(runTimes == 0) return num;
return add100andDouble( (num + 100 ) * 2, runTimes - 1 );
}
$(function(){
var number = parseFloat(prompt("Enter a number"));
alert( add100andDouble(number, 10) );
});
var generate = function generate(rate){
var array = [];
s=0;
for (var i = 0; i < 10; i++){
s = rate + (array[i-1] ? array[i-1] :1000);
array.push(s*2);
}
return array;
}
console.log(generate(100));
I suggest to use a function f(x) for calculating the new value. It makes the code more clearly arranged.
function f(x) {
return (x + 100) * 2;
}
var i = 0,
x = 1000,
array = [];
for (i = 0; i < 10; i++) {
array.push(x);
x = f(x);
}
document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');
Recursion has its merits on other solutions but I would go with plain old for loop for this situation.
Assuming rate is the starting number, and the return is a fixed 10 value series:
function generate(rate) {
var series = [];
for(var i = 0; i < 10; i++) {
series.push(rate = ((rate + 100) * 2));
};
return series;
}
It can be easily modified to send the series length if needed.

How to divide number into integer pieces that are each a multiple of n?

Had a hard time coming up with a concise title for this. I'm sure there are terms for what I want to accomplish and there is no doubt a common algorithm to accomplish what I'm after - I just don't know about them yet.
I need to break up a number into n pieces that are each a multiple of 50. The number is itself a multiple of 50. Here is an example:
Divide 5,000 by 3 and end up with three numbers that are each multiples of 50:
1,650
1,700
1,650
I also would like to have the numbers distributed so that they flip back and forth, here is an example with more numbers to illustrate this:
Divide 5,000 by 7 and end up with 7 numbers that are each multiples of 50:
700
750
700
750
700
700
700
Note that in the above example I'm not worried that the extra 50 is not centered in the series, that is I don't need to have something like this:
700
700
750 <--- note the '50s' are centered
700
750 <--- note the '50s' are centered
700
700
Hopefully I've asked this clearly enough that you understand what I want to accomplish.
Update: Here is the function I'll be using.
var number = 5000;
var n = 7;
var multiple = 50;
var values = getIntDividedIntoMultiple(number, n, multiple)
function getIntDividedIntoMultiple(dividend, divisor, multiple)
{
var values = [];
while (dividend> 0 && divisor > 0)
{
var a = Math.round(dividend/ divisor / multiple) * multiple;
dividend -= a;
divisor--;
values.push(a);
}
return values;
}
var number = 5000;
var n = 7;
var values = [];
while (number > 0 && n > 0) {
var a = Math.floor(number / n / 50) * 50;
number -= a;
n--;
values.push(a);
} // 700 700 700 700 700 750 750
Edit
You can alternate Math.floor and Math.ceil to obtain the desired result:
while (number > 0 && n > 0) {
if (a%2 == 0)
a = Math.floor(number / n / 50) * 50;
else
a = Math.ceil(number / n / 50) * 50;
number -= a;
n--;
values.push(a);
} // 700 750 700 750 700 700 700
// i - an integer multiple of k
// k - an integer
// n - a valid array length
// returns an array of length n containing integer multiples of k
// such that the elements sum to i and the array is sorted,
// contains the minimum number of unique elements necessary to
// satisfy the first condition, the elements chosen are the
// closest together that satisfy the first condition.
function f(i, k, n) {
var minNumber = (((i / k) / n) | 0) * k;
var maxNumber = minNumber + k;
var numMax = (i - (minNumber * n)) / k;
var nums = [];
for (var i = 0; i < n - numMax; ++i) {
nums[i] = minNumber;
}
for (var i = n - numMax; i < n; ++i) {
nums[i] = maxNumber;
}
return nums;
}
So your second example would be
f(5000, 50, 7)
which yields
[700,700,700,700,700,750,750]
Let a be your starting number, k - number of parts you want to divide to.
Suppose, that b = a/n.
Now you want to divide b into k close integer parts.
Take k numbers, each equal to b/k (integer division).
Add 1 to first b%k numbers.
Multiply each number by n.
Example:
a = 5000, n = 50, k = 7.
b = 100
Starting series {14, 14, 14, 14, 14, 14, 14}
Add 1 to first 2 integers {15, 15, 14, 14, 14, 14, 14}.
Multiply by 50 {750, 750, 700, 700, 700, 700, 700}.
Your problem is the same as dividing a number X into N integer pieces that are all within 1 of each other (just multiply everything by 50 after you've found the result). Doing this is easy - set all N numbers to Floor(X/N), then add 1 to X mod N of them.
I see your problem as basically trying to divide a sum of money into near-equal bundles of bills of a certain denomination.
For example, dividing 10,000 dollars into 7 near-equal bundles of 50-dollar bills.
function getBundles(sum, denomination, count, shuffle)
{
var p = Math.floor(sum / denomination);
var q = Math.floor(p / count);
var r = p - q * count;
console.log(r + " of " + ((q + 1) * denomination)
+ " and " + (count - r) + " of " + (q * denomination));
var b = new Array(count);
for (var i = 0; i < count; i++) {
b[i] = (r > 0 && (!shuffle || Math.random() < .5 || count - i == r)
? (--r, q + 1) : q)
* denomination;
}
return b;
}
// Divide 10,000 dollars into 7 near-equal bundles of 50-dollar bills
var bundles = getBundles(10000, 50, 7, true);
console.log("bundles: " + bundles);
Output:
4 of 1450 and 3 of 1400
bundles: 1400,1450,1450,1400,1450,1400,1450
If the last argument shuffle is true, it distributes the extra amount randomly between the bundles.
Here's my take:
public static void main(String[] args) {
System.out.println(toList(divide(50, 5000, 3)));
System.out.println(toList(divide(50, 5000, 7)));
System.out.println(toList(divide(33, 6600, 7)));
}
private static ArrayList<Integer> toList(int[] args) {
ArrayList<Integer> list = new ArrayList<Integer>(args.length);
for (int i : args)
list.add(i);
return list;
}
public static int[] divide(int N, int multiplyOfN, int partsCount) {
if (N <= 0 || multiplyOfN <= N || multiplyOfN % N != 0)
throw new IllegalArgumentException("Invalid args");
int factor = multiplyOfN / N;
if (partsCount > factor)
throw new IllegalArgumentException("Invalid args");
int parts[] = new int[partsCount];
int remainingAdjustments = factor % partsCount;
int base = ((multiplyOfN / partsCount) / N) * N;
for (int i = 0; i < partsCount; i ++) {
parts[i] = (i % 2 == 1 && remainingAdjustments-- > 0) ? base + N : base;
}
return parts;
}
My algorithm provides even distribution of remainder across parts:
function splitValue(value, parts, multiplicity)
{
var result = [];
var currentSum = 0;
for (var i = 0; i < parts; i++)
{
result[i] = Math.round(value * (i + 1) / parts / multiplicity) * multiplicity - currentSum;
currentSum += result[i];
}
return result;
}
For value = 5000, parts = 7, multiplicity = 50 it returns
[ 700, 750, 700, 700, 700, 750, 700 ]

Math.ceil to nearest five at position 1

Okay....
I have a lot of uncontrolled numbers i want to round:
51255 -> 55000
25 -> 25
9214 -> 9500
13135 -> 15000
25123 -> 30000
I have tried modifying the numbers as string and counting length....
But is there a simple way using some Math function maybe?
Here's my late answer. Uses no Math methods.
function toN5( x ) {
var i = 5;
while( x >= 100 ) {x/=10; i*=10;}
return ((~~(x/5))+(x%5?1:0)) * i;
}
DEMO: http://jsbin.com/ujamoj/edit#javascript,live
[51255, 24, 25, 26, 9214, 13135, 25123, 1, 9, 0].map( toN5 );
// [55000, 25, 25, 30, 9500, 15000, 30000, 5, 10, 0]
Or this is perhaps a bit cleaner:
function toN5( x ) {
var i = 1;
while( x >= 100 ) {x/=10; i*=10;}
return (x + (5-((x%5)||5))) * i;
}
DEMO: http://jsbin.com/idowan/edit#javascript,live
To break it down:
function toN5( x ) {
// v---we're going to reduce x to the tens place, and for each place
// v reduction, we'll multiply i * 10 to restore x later.
var i = 1;
// as long as x >= 100, divide x by 10, and multiply i by 10.
while( x >= 100 ) {x/=10; i*=10;}
// Now round up to the next 5 by adding to x the difference between 5 and
// the remainder of x/5 (or if the remainder was 0, we substitute 5
// for the remainder, so it is (x + (5 - 5)), which of course equals x).
// So then since we are now in either the tens or ones place, and we've
// rounded to the next 5 (or stayed the same), we multiply by i to restore
// x to its original place.
return (x + (5-((x%5)||5))) * i;
}
Or to avoid logical operators, and just use arithmetic operators, we could do:
return (x + ((5-(x%5))%5)) * i;
And to spread it out a bit:
function toN5( x ) {
var i = 1;
while( x >= 100 ) {
x/=10;
i*=10;
}
var remainder = x % 5;
var distance_to_5 = (5 - remainder) % 5;
return (x + distance_to_5) * i;
}
var numbers = [51255, 25, 9214, 13135, 25123, 3, 6];
function weird_round(a) {
var len = a.toString().length;
var div = len == 1 ? 1 : Math.pow(10, len - 2);
return Math.ceil(a / 5 / div) * div * 5;
}
alert(numbers.map(weird_round));
Also updated for numbers below 10. Won't work properly for negative numbers either, just mention if you need this.
DEMO
I'm not sure why, but I thought it would be fun with regular expressions:
var result = +(number.toString().replace(/([1-9])([0-9])(.+)/, function() {
return Math.ceil(+(arguments[1] + '.' + arguments[2])) * 10 - (+arguments[2] < 5?5:0) + arguments[3].replace(/./g, '0');
}));
Working Demo
with(Math) {
var exp = floor(log(number)/log(10)) - 1;
exp = max(exp,0);
var n = number/pow(10,exp);
var n2 = ceil(n/5) * 5;
var result = n2 * pow(10,exp);
}
http://jsfiddle.net/NvvGf/4/
Caveat: only works for the natural numbers.
function round(number) {
var numberStr = number + "",
max,
i;
if (numberStr[1] > '4') {
numberStr[0] = parseInt(numberStr[0]) + 1;
numberStr[1] = '0';
} else {
numberStr[1] = '5';
}
for (i = 2; max = numberStr.length; i < max; i += 1) {
numberStr += '0';
}
return parseInt(numberStr);
}
Strange coincidence, I wrote something really similar not so long ago!
function iSuckAtNames(n) {
var n = n.toString(), len = n.length, res;
//Check the second number. if it's less than a 5, round down,
//If it's more/equal, round up
//Either way, we'll need to use this:
var res = parseFloat(n[0]) * Math.pow(10, len - 1); //e.g. 5 * 10^4 = 50000
if (n[1] <= 5) {
//we need to add a 5 right before the end!
res += 5 * Math.pow(10, len - 2);
}
else {
//We need another number of that size
res += Math.pow(10, len - 1);
}
return res;
}

Categories

Resources