Python 3: How to generate random float (in two decimal places) between 10.00 and 100.00 - python-3.x

I need help generating random float (in two decimal places) between 10.00 and 100.00 And I would like to do it without using the print function.
I tried random.choice(range(10.10, 200.00)) but it does not work, because parameters have to be integers.
random.uniform(1, 10) does not seem to have an option for trimming the decimals.

Related

How to calculate change in coins in python

I was given a prompt that asks to input any amount of money and print how many of each coin is needed for that amount of money. For example, 2.05 would be 8 quarters and 1 nickel. I thought I had it right, but when I input 2.05, it prints 8 quarters and 4 pennies. Here is what I have so far:
cents=float(input("How much change is owed?"))
quarters=cents//.25
round(quarters)
print(quarters, "quarters")
cents=cents%.25
dimes=cents//.10
round(dimes)
print(dimes, "dimes")
cents=cents%.10
nickels=cents//.05
round(nickels)
print(nickels, "nickels")
cents=cents%.05
pennies=cents//.01
round(pennies)
print(pennies, "pennies")
How do I fix it? Also, how would I write a function so that the amount of each coin is written as an integer and not as a float?

python return wrong result when I multiple float with int

I have a multiple in python 3.7.3
when I run 0.58 * 100 it return 57.99999999999999
Then I found that Java have same result. But C can return right number. I don't know what happen with them. Sorry if it look like basic.
Its actually not the wrong answer, just an unexpected one.
If we think a bit about the problem, There are an infinite amount of numbers between 0 and 1. Then we can see that you cannot represent all numbers between 0 and 1 with a finite amount of bytes, as infinite numbers are more then a finite number of numbers. so some numbers just cant be represented (in fact, most numbers of the infinite series between 0 and 1 cannot be represented)
Following the floating point standard (IEEE-754), 0.58 is really 0.5799999999999999289457264239899814128875732421875 which is the closest number to 0.58 that can be represented with 64bit floating points.
check it with python
>>> Decimal(0.58)
Decimal('0.57999999999999996003197111349436454474925994873046875')
If you want 58.0 you can quantize it to two decimals with the Decimal class.
>>> Decimal(100 * 0.58).quantize(Decimal('.01'))
Decimal('58.00')

Removing ".00" from Decimal format numbers to make them whole numbers keeping other decimal numbers intact

I have a column that has Amount rounded to 2 decimal places. So there are two types of enteries, one of the form 359.00 and others 359.78. I want to remove .00 from the first form of enteries to make it interger(359) keeping the decimal format of other number intact.
Data are floating numbers
A column don't have mixed types. Assuming your data are stored as floating numbers (dtype: float64), they will remain floats. What you can do is to use a custom format so that they are shown on the screen as you wish, but internally they remains floats. For example:
dfa = pd.DataFrame.from_records([(1,), (3.34,), (2.49,), (5,), (7,)], columns=['Amount'])
pd.options.display.float_format = lambda x : "{:2.2f}".format(x).rstrip('0').rstrip('.')
print(dfa)
This prints:
Amount
0 1
1 3.34
2 2.49
3 5
4 7
Data are strings
If instead your data are just strings representing numbers (dtype: object) you can use pandas.applymap to actually edit the strings according to a format style.
dfb = pd.DataFrame.from_records([("1.00",), ("3.34",), ("2.49",), ("5.00",), ("7.00",)], columns=['Amount'])
dfbb = dfb.applymap(lambda x : str(x).rstrip('0').rstrip('.'))
print(dfbb)
This prints (again):
Amount
0 1
1 3.34
2 2.49
3 5
4 7
Data are floating numbers but you want strings
In this case you can combine the two methods:
dfc = dfa.applymap(lambda x : "{:2.2f}".format(x).rstrip('0').rstrip('.'))
print(dfc)
Starting from a dataframe with floating numbers, you end with a dataframe of formatted strings. It prints the same, no need to touch pandas format settings.
Credits to this answer for the basic idea.

Normalized values, when summed are more than 1

I have two files:
File 1:
TOPIC:topic_0 1294
aa 234
bb 123
TOPIC:topic_1 2348
aa 833
cc 239
bb 233
File 2:
0.1 0.2 0.3 0.4
This is just the format of my files. Basically, when the second column (omitting the first "TOPIC" line) is summed for each topic, it constitutes to 1 as they are the normalized values. Similarly, in file 2, the values are normalized and hence they also constitute to 1.
I perform multiplication of the values from file 1 and 2. The resulting output file looks like:
aa 231
bb 379
cc 773
The second column when summed of the output file should give 1. But few files have values little over 1 like 1.1, 1.00038. How can I precisely get 1 for the output file? Is it some rounding off that I should do or something?
PS: The formats are just examples, the values and words are different. This is just for understanding purposes. Please help me sort this.
Python stores floating point decimals in base-2.
https://docs.python.org/2/tutorial/floatingpoint.html
This means that some decimals could be terminating in base-10, but are repeating in base-2, hence the floating-point error when you add them up.
This gets into some math, but imagine in base-10 trying to express the value 2/6. When you eliminate the common factors from the numerator and denominator it's 1/3.
It's 0.333333333..... repeating forever. I'll explain why in a moment, but for now, understand that if only store the first 16 digits in the decimal, for example, when you multiply the number by 3, you won't get 1, you'll get .9999999999999999, which is a little off.
This rounding error occurs whenever there's a repeating decimal.
Here's why your numbers don't repeat in base-10, but they do repeat in base-2.
Decimals are in base-10, which prime factors out to 2^1 * 5^1. Therefore for any ratio to terminate in base-10, its denominator must prime factor to a combination of 2's and 5's, and nothing else.
Now let's get back to Python. Every decimal is stored as binary. This means that in order for a ratio's "decimal" to terminate, the denominator must prime factor to only 2's and nothing else.
Your numbers repeat in base-2.
1/10 has (2*5) in the denominator.
2/10 reduces to 1/5 which still has five in the denominator.
3/10... well you get the idea.

Limit Decimal Places In Generated Numbers in R.Studio

I am a beginner in programmin in general and R specifically.
I would like to generate a set of random numbers in a normal distribution but to limit the decimal places in these numbers to only 2.
I have been using x1 <- runif() to generate my numbers.
Can I add something to it to enable me to only get results rounded off to 2 decimal places?
You can limit the decimal places using the round() function.
If I understand your question correctly this should do the trick:
x1 <- round(
runif(5, min=0, max=1)
, digits = 2
)
x1
The results, which will be different each time, are:
[1] 0.55 0.55 0.75 0.85 0.13

Resources