How to generate random Double numbers in Google Sheets within range? - google-sheets

I am looking to generate a list of 50 numbers between -.15 to +.15 in Google Sheets
Both rand(), randarray() seem to offer positive numbers only. I hacked my way around n via a conditional =if(F5>=0.5,1,-1), but this seems grossly inelegant.
Is there a better, cleaner way to create a list of positive and negative numbers within a given range? Wish =RANDBETWEEN(-0.15,0.15) would work
Any ideas?

May be
=RAND()*0.3-0.15
so that you will have numbers between -0.15 and +0.15. Or to avoid decimals after .01
=round(RAND()*0.3-0.15,2)

Related

Advance range sorting options not sorting in order for numbers

Something strange is happening with the sorting. I am sorting G (weight) from smallest to largest. The numbers are 1 to 41.
It doesn't sort starting from the number 1. Instead the order is 10,11,12,13,14,15,16,17,18,19,2,20,21,23,25,26,3,30,35,4,41,5,6,7,8,9.
I know technically 10 starts with the number one, but shouldn't this application be smart enough to know that you don't start counting at number 10? The smallest weight of 2 should be first, followed 3,4,5,67,8,9,10,11,12,etc.
Is this not possible with that sorting option?
try to multiply your G column with 1 to convert your string numbers into numeric numbers as of, you are sorting it now by alphabetical order 10,1,20,2 not numerical 1,2,10,20
It sounds like your Col-G weights are strings and not actual numbers. And assuming that is the case, Google sorts in order according to ASCII char numbers. If you have any non-numeric characters appended (e.g., "kg"), then you've created strings automatically. However, if you only have digits and they somehow got into your sheet as strings (which are not actively generated by formula), you can try selecting that column and choosing Format > Number > 0.

Why is this Google Sheets Concatenate Formula giving me weird results?

I'm trying to use Google Sheets to concatenate a bit of data. It works 90% of the time, however on certain numbers, I get an odd result. I have to copy the result of this data and paste it into a financial program in a specific format and am using the concatenate formula to do this. The format the program requires is that each field be separated by one period, even if it is a dollar amount as the program will automatically move the decimal point two places to the left while it is evaluating the information. The issue is that on some numbers the formula adds two periods between the fields, which stops the evaluation of the data in our financial program.
Here is a screenshot including the formula
You can see that it works with most numbers in the amount column, but with two of the amounts and several others, it adds two periods after the amount.
Would you please take a look at this and see if you can help me find the issue?
Thank you!!!!
Looks like it's an existing floating point calculation error in Google Sheets, the multiplication by 100 did not return exact value for certain numbers but with extra very small decimal. That's why there's an additional period on the result.
As a workaround, use ROUND() upon multiplying by 100 to "snap" it to an integer.
Sample:
References:
Floating Point Calculation Error
use just:
=B2&"."&ROUND(C2*100)&"."&D2

How to convert an exclusive range to inclusive range in Ruby?

I have an exclusive range of integers, e.g.
range = 1000...10001
I would rather work with inclusive ranges all the way, but the reason I have this is because when I tell ActiveRecord to store an inclusive range in PostgreSQL, I get the exclusive version when I query it back.
I want to convert it into an inclusive range, e.g. 1000..10000.
I'm doing:
(range.begin)..(range.end-1)
However it feels clunky and un-ruby-ish.
My whole API relies on passing ranges, but I'm also considering storying 2 values or storing an array in Postgres instead of a range.
Is there a better way to do this?
Edit (some clarifications)
I want to do this as efficiently as possible, given the large difference between the ranges
My use case is to use the begin and end part of the ranges. I'm not actually iterating over them or checking inclusion. I just thought that a ruby range is a nice way to represent the actual range for e.g. displaying a price ($ 1.000 - $ 10.000)
I would use Range#min and Range#max:
exclusive_range = (1000...10001)
inclusive_range = (exclusive_range.min..exclusive_range.max)
#=> 1000..10000

Google sheets - Subtraction giving unexpected results

I have the following sheet:
Why does the difference of two cells with the same value (J7 & J8) not equal zero?
Here is an example spreadsheet.
Switching to Format/Number/Scientific on that cell, you can see the leftover value:
These rounding errors are clearly a regular thing. You can use a formula such as =E3-round(E3,2) to see them for any cell:
Clearly, allowing for a small value in the conditional formatting would avoid the problem:
To add to #df778899's answer, this is not just a "regular thing". It is an unavoidable consequence of floating-point arithmetics.
The surprising thing is that Google apparently represents numbers internally in the floating-point format. For the same numbers, Excel gives the correct result in the equality comparison, which suggests that unlike Google, Excel's representation is fixed-point.
EDIT: upon double-checking, one can find examples of floating point errors in Excel too, so this paragraph is not correct.
Students in computer science are taught to always use approximated and not exact comparison for FP numbers. I think it's unreasonable to expect this from spreadsheets users, though.
In practical terms, when comparing fractionals to other fractionals, avoid using
=if(j7=j8, ...)
or EQ(). Instead, use
=if(abs(J7-J8)<0.0001, ...)
(with appropriate arbitrary precision)

How to format cells as weight in grams but removing insignificant decimal point and places?

I have a sheet with values that I want to format in grams. The values range from high to low and I wish to format them with a comma as a thousand-separator, and rounded to two decimal places, but trimming both the decimal point and places where the number is a whole number. The following examples should explain better:
1000 to be presented as 1,000g
0.75 to be presented as 0.75g
0.2 to be presented as 0.2g
0.1234 to be presented as 0.12g
I've tried using a custom number format of #,##0.##g but this does not satisfy my first requirement (a) where the numbers are whole numbers and leaves an insignificant decimal point (i.e. 1,000.g), although does very well at formatting the remaining three requirements (b, c and d).
Is there a way of overcoming this?
#,##0.00_ g;(#,##0.00 g)
I'm a bit late to this article but I found the above format works for me.
Assuming your numbers are in column A with a header. You could try putting this in B2 then hide column A:
=arrayformula(if(arrayformula(TRUNC(A2:A,2)&"g")="0g","",arrayformula(TRUNC(A2:A,2)&"g")))
The best I could do with format was [=0]0;.## It gets rid of the period in 1000 but does not truncate 0.1234 and doesn't add the g. Maybe you could work with that if you must use format. I really don't think format can do it.

Resources