I was doing a problem and using the function rnd() but the random values <0.001359 were making other values not acceptable.
How to generate random numbers beginning at 0.001359 until 1?
This will give you a random number greater than or equal to 0.001359 and less than 1.
0.001359 + Rnd() * 0.998641
Try to generate numbers between 1359 and 1000000, and then divide the result by 1000000.
The command for generating the random numbers will be
Int ((1000000 - 1359 + 1) * Rnd + 1359)
For more information look here.
Try this formula (Excel 2013):
=(RANDBETWEEN(1359,1000000)) / 1000000
Since it only returns whole numbers, just slide the decimal as needed and then divide the result to get back to you decimal.
To do that in VBA, you can use this:
randomnumber = WorksheetFunction.RandBetween(1359, 1000000) / 1000000
Related
I have some product prices like
30,56
25,34
26,88
30,13
I want to to round them with a 0,50 limit
if the number is over x.50 to make it x.90 and if not make it x.50
is it possible with a function of VBA?
Alternate solution:
=INT(A1)+0.5+0.4*(MOD(A1,1)>0.5)
Use this formula to round:
=IF(A:A-INT(A:A)>0.5,INT(A:A)+0.9,INT(A:A)+0.5)
Explanation
It subtracts the integer part of the floating number so and tests if this is >0.5 so A:A-INT(A:A)>0.5 means (30.56 - 30) > 0.5 which is0.56 > 0.5
The formula means something like that:
If (30.56 - 30) > 0.5 Then (30 + 0.9) Else (30 + 0.5)
Use IF, MOD and RoundDown
=IF(MOD(A2,1)>0.5,ROUNDDOWN(A2,0)+0.9,ROUNDDOWN(A2,0)+0.5)
You may want additional conditions to handle fringe cases like a price of 0.
I am trying to understand the maths in this code that converts binary to decimal. I was wondering if anyone could break it down so that I can see the working of a conversion. Sorry if this is too newb, but I've been searching for an explanation for hours and can't find one that explains it sufficently.
I know the conversion is decimal*2 + int(digit) but I still can't break it down to understand exaclty how it's converting to decimal
binary = input('enter a number: ')
decimal = 0
for digit in binary:
decimal= decimal*2 + int(digit)
print(decimal)
Here's example with small binary number 10 (which is 2 in decimal number)
binary = 10
for digit in binary:
decimal= decimal*2 + int(digit)
For for loop will take 1 from binary number which is at first place.
digit = 1 for 1st iteration.
It will overwrite the value of decimal which is initially 0.
decimal = 0*2 + 1 = 1
For the 2nd iteration digit= 0.
It will again calculate the value of decimal like below:
decimal = 1*2 + 0 = 2
So your decimal number is 2.
You can refer this for binary to decimal conversion
The for loop and syntax are hiding a larger pattern. First, consider the same base-10 numbers we use in everyday life. One way of representing the number 237 is 200 + 30 + 7. Breaking it down further, we get 2*10^2 + 3*10^1 + 7*10^0 (note that ** is the exponent operator in Python, but ^ is used nearly everywhere else in the world).
There's this pattern of exponents and coefficients with respect to the base 10. The exponents are 2, 1, and 0 for our example, and we can represent fractions with negative exponents. The coefficients 2, 3, and 7 are the same as from the number 237 that we started with.
It winds up being the case that you can do this uniquely for any base. I.e., every real number has a unique representation in base 10, base 2, and any other base you want to work in. In base 2, the exact same pattern emerges, but all the 10s are replaced with 2s. E.g., in binary consider 101. This is the same as 1*2^2 + 0*2^1 + 1*2^0, or just 5 in base-10.
What the algorithm you have does is make that a little more efficient. It's pretty wasteful to compute 2^20, 2^19, 2^18, and so on when you're basically doing the same operations in each of those cases. With our same binary example of 101, they've re-written it as (1 *2+0)*2+1. Notice that if you distribute the second 2 into the parenthesis, you get the same representation we started with.
What if we had a larger binary number, say 11001? Well, the same trick still works. (((1 *2+1 )*2+0)*2+0)*2+1.
With that last example, what is your algorithm doing? It's first computing (1 *2+1 ). On the next loop, it takes that number and multiplies it by 2 and adds the next digit to get ((1 *2+1 )*2+0), and so on. After just two more iterations your entire decimal number has been computed.
Effectively, what this is doing is taking each binary digit and multiplying it by 2^n where n is the place of that digit, and then summing them up. The confusion comes due to this being done almost in reverse, let's step through an example:
binary = "11100"
So first it takes the digit '1' and adds it on to 0 * 2 = 0, so we
have digit = '1'.
Next take the second digit '1' and add it to 1* 2 =
2, digit = '1' + '1'*2.
Same again, with digit = '1' + '1'*2 +
'1'*2^2.
Then the 2 zeros add nothing, but double the result twice,
so finally, digit = '0' + '0'*2 + '1'*2^2 + '1'*2^3 + '1'*2^4 = 28
(I've left quotes around digits to show where they are)
As you can see, the end result in this format is a pretty simple binary to decimal conversion.
I hope this helped you understand a bit :)
I will try to explain the logic :
Consider a binary number 11001010. When looping in Python, the first digit 1 comes in first and so on.
To convert it to decimal, we will multiply it with 2^7 and do this till 0 multiplied by 2^0.
And then we will add(sum) them.
Here we are adding whenever a digit is taken and then will multiply by 2 till the end of loop. For example, 1*(2^7) is performed here as decimal=0(decimal) +1, and then multiplied by 2, 7 times. When the next digit(1) comes in the second iteration, it is added as decimal = 1(decimal) *2 + 1(digit). During the third iteration of the loop, decimal = 3(decimal)*2 + 0(digit)
3*2 = (2+1)*2 = (first_digit) 1*2*2 + (seconds_digit) 1*2.
It continues so on for all the digits.
I've seen multiple answers say the following algorithm works fine to generate a random number between two values. I'm getting spurious results where I'll sometimes get a value returned that is higher than the upper bound.
Dim random as Integer
random = Int (3 - 0 + 1) * Rnd + 0
Debug.Print random
This should give values between 0 - 3 right? I'm seeing 0 to 4 when run a few times..???
This is one of the idiosyncrasies of VBA.
You can see the effect more clearly by writing
random = Int(4) * Rnd
4 * Rnd is a floating point double type, and when it gets assigned to random, The same rounding convention as for CInt is applied1; i.e. if Int(4) * Rnd is 3.5 or greater, the result is 4.
The fix is to write
random = Int(4 * Rnd)
1 The convention "round half to even" is often called Banker's Rounding. See
https://en.wikipedia.org/wiki/Rounding#Round_half_to_even
I think it is to be expected when explicitly converting floating point number to integer, but what might not be expected is that the rounding is towards the closest even number:
Dim i As Integer
i = 3.5 ' i = 4
i = "2.5" ' i = 2
I am trying to find the formula to calculate the maximum bit-width required to contain a sum of M n-bit unsigned binary numbers. Thanks!
The maximum bit-width needed should be ceil(log_2(M * (2^n - 1))).
Edit: Thanks to #MBurnham I realize now that it should be floor(log_2(M * (2^n - 1))) + 1 instead.
Assuming positive integers, you need floor(log2(x)) + 1 bits to store x. and the largest value the sum of m n-bit numbers can produce would be m * 2^n.
So I believe the formula should be
floor(log2(m * 2^n)) + 1
bits.
If I add 2 numbers the I need 1 bit more than the wider of the 2 numbers to store the result. So, if I add 2 n-bit numbers, I need n+1 bits to store the result.
if I add another n-bit number, I need (n+1)+1 bits to store the result (that's 3 n-bit numbers added so far)
if I add another n-bit number, I need ((n+1)+1)+1 bits to store the result (that's 4 n-bit numbers added so far)
if I add another n-bit number, I need (((n+1)+1)+1)+1 bits to store the result (that's 5 n-bit numbers added so far)
So, I think your formula is
n + M - 1
Probably the title is not very suggestive.
Let me explain you with an example. I have:
12345.6
2345.1
12345.00000001
I want those numbers to be roundup to 12350.
How can I do this?
If possible, I would rather use formulas instead of VBA.
You could also use CEILING which rounds up to an integer or desired multiple of significance
ie
=CEILING(A1,10)
rounds up to a multiple of 10
12340.0001 will become 12350
Use ROUND but with num_digits = -1
=ROUND(A1,-1)
Also applies to ROUNDUP and ROUNDDOWN
From Excel help:
If num_digits is greater than 0 (zero), then number is rounded to the specified number of decimal places.
If num_digits is 0, then number is rounded to the nearest integer.
If num_digits is less than 0, then number is rounded to the left of the decimal point.
EDIT:
To get the numbers to always round up use =ROUNDUP(A1,-1)
You can use the function MROUND(<reference cell>, <round to multiple of digit needed>).
Example:
For a value A1 = 21 round to multiple of 10 it would be written as
=MROUND(A1,10)
for which Result = 20
For a value Z4 = 55.1 round to multiple of 10 it would be written as
=MROUND(Z4,10)
for which Result = 60
the second argument in ROUNDUP, eg =ROUNDUP(12345.6789,3) refers to the negative of the base-10 column with that power of 10, that you want rounded up. eg 1000 = 10^3, so to round up to the next highest 1000, use ,-3)
=ROUNDUP(12345.6789,-4) = 20,000
=ROUNDUP(12345.6789,-3) = 13,000
=ROUNDUP(12345.6789,-2) = 12,400
=ROUNDUP(12345.6789,-1) = 12,350
=ROUNDUP(12345.6789,0) = 12,346
=ROUNDUP(12345.6789,1) = 12,345.7
=ROUNDUP(12345.6789,2) = 12,345.68
=ROUNDUP(12345.6789,3) = 12,345.679
So, to answer your question:
if your value is in A1, use
=ROUNDUP(A1,-1)