Split number into n numbers [duplicate] - python

This question already has answers here:
Generating a list of random numbers, summing to 1
(12 answers)
Generate random numbers summing to a predefined value
(7 answers)
Getting N random numbers whose sum is M
(9 answers)
Closed 10 months ago.
Let's say I have a number m (it will be integer, usually 1) and I want to split m into n random numbers, which have the sum m. I want to make a distribution which will be random, but will be close (doesn't have to be really close) to uniform distribution.
I have this
pieces = []
for i in range(n-1):
pieces.append(random.uniform(0,m-sum(pieces)))
pieces.append(m-sum(pieces))
The problem is, that the first number is usually really high compared to the rest of the numbers. When I tried it for m = 1 and let's say n=10, the first number was close to 0,5 and the last number was close to like 0,001 (or something like that), which is not a problem, but the problem is, that the first number is on average really big compared to all of the other numbers.
My question is: Is there some kind of way to do this, so the numbers are closer? There can be a big difference between biggest and smallest number, but I want it to feel more uniform, if that makes sense.

Related

Problem with a single line code of random digit number [duplicate]

This question already has answers here:
How to generate a random number with a specific amount of digits?
(10 answers)
Closed 2 years ago.
I took this line from online to make a random 4 digit number
number = str(random.randint(0,9999))
but sometimes it give me random 3 digit number why?
Is their a better way to make a random multiple digit number in python
number = str(random.randint(1000,9999))
Because you've asked for a number between 0 and 9999 inclusive. That includes single, double and triple digit numbers as well.
If you want only 4 digit numbers start at 1000:
number = str(random.randint(1000, 9999))
See the randint() documentation.
If you want 4 digits that could include leading zeroes then you use random.choices():
from string import digits
number = ''.join(random.choices(digits, k=4))
which picks 4 digits with replacement and joins them into a string.

Do numbers, in random module, have the same chance to appear or not to appear? [duplicate]

This question already has answers here:
How do I create a list of random numbers without duplicates?
(21 answers)
Generate random integers between 0 and 9
(22 answers)
Is Random function (in any programming language) biased?
(2 answers)
Closed 2 years ago.
i want to build a program in Python designed for generating hazard numbers in lotto. For example from 1 to 45 , i want that every number between 1 and 45 ( 1 and 45 included) have the same chance to appear or not, like in real life.
What is ( or what are) the appropriate random that can do that or closer to do that (approximation).
EXEMPLE
value= random.randint(1,46)
Does all the numbers between 1 and 45 have the same chance to appear or to not appear like in reel world ?
Does chance in computer world is like chance in reel world, if no , how to make it closer?
It is not about duplication.
Thank you for helping
You want to sample k numbers, without replacement, from a discrete uniform distribution on the interval [1, 45].
Let's say you want to pick 6 numbers. Then write
import random
winners = random.sample(range(1, 46), k=6)
Then winners will be a list of six randomly chosen numbers.
Explanation:
range(1, 45) represents the interval [1, 45). In other words, it wouldn't include 45. So the second argument is 46.
sample chooses k numbers from that range of 45 without replacement. In other words, just like in the typical lottery, no number will be chosen twice — which you can't count on with other random module functions.
The distribution will be uniform by default; each number is equally likely to be chosen.

F.monotonically_increasing_id() returns long random [duplicate]

This question already has answers here:
Using monotonically_increasing_id() for assigning row number to pyspark dataframe
(6 answers)
Closed 3 years ago.
x = df.withColumn("id_col", F.monotonically_increasing_id())
returns random long integers instead of sorted int numbersenter image description here
What you are seeing is the expected behavior of the function. From the documentation
The generated ID is guaranteed to be monotonically increasing and
unique, but not consecutive.
The current implementation puts the partition ID in the upper 31 bits, and the record number
within each partition in the lower 33 bits. The assumption is that the data frame has
less than 1 billion partitions, and each partition has less than 8 billion records
This is why you see long random integers. They may not be sequential but they are in increasing order and for all practical purposes, unique.

How to get index of maximum value of each consecutive 4 numbers in a list of python? [duplicate]

This question already has answers here:
how do I calculate a rolling idxmax
(6 answers)
Closed 6 years ago.
I have a list L. It has random numbers.
I want to get the index of maximum number among each 4 numbers of L.
What is the way to do it ?
I can get the max value from below (i guess ) but not sure how to get index value ? Also is there a way to do same in nd array ???
import pandas as pd
a=np.random.rand(100)
L=list(a)
pd.rolling_max(L, 4)
There is no quicker way than to run through the list looking at the 4 size subsets. For each subset, you can have a variable that stores the index of the maximum number seen so far (among the 4). This is O(n) which is the quickest you can do it because you will have to look at each number in the list at least once.

How can I replace a integer that shows up twice in a random list [duplicate]

This question already has answers here:
Generate 'n' unique random numbers within a range [duplicate]
(4 answers)
Closed 7 years ago.
I have a list that looks like this:
l = [random.randint(0,9),random.randint(0,9),random.randint(0,9),random.randint(0,9)]
but if it outputs something like this[9,0,5,5] what can I do replace the repeating integer?
If the goal is to get four unique items in random order, let Python do the work for you:
l = random.sample(range(10), 4)
random.sample is intended specifically for "random sampling without replacement", which appears to be the goal of your code.

Categories