random

Python Generate a random Maxwell distribution from a normal distribution

╄→尐↘猪︶ㄣ 提交于 2020-05-28 04:14:07
问题 I have a set of data that follows a normal distribution in which I can fit the histogram and obtain the mean and sigma. For the sake of example, I will approximate it by generating a random normal distribution as follows: from scipy.stats import maxwell import math import random import numpy as np import matplotlib.pyplot as plt from scipy.stats import norm from scipy.optimize import curve_fit from IPython import embed # put embed() where you want to stop import matplotlib.ticker as ticker

python seed() not keeping same sequence

微笑、不失礼 提交于 2020-05-27 03:07:49
问题 I'm using a random.seed() to try and keep the random.sample() the same as I sample more values from a list and at some point the numbers change.....where I thought the one purpose of the seed() function was to keep the numbers the same. Heres a test I did to prove it doesn't keep the same numbers. import random a=range(0,100) random.seed(1) a = random.sample(a,10) print a then change the sample much higher and the sequence will change(at least for me they always do): a = random.sample(a,40)

python seed() not keeping same sequence

倾然丶 夕夏残阳落幕 提交于 2020-05-27 03:07:26
问题 I'm using a random.seed() to try and keep the random.sample() the same as I sample more values from a list and at some point the numbers change.....where I thought the one purpose of the seed() function was to keep the numbers the same. Heres a test I did to prove it doesn't keep the same numbers. import random a=range(0,100) random.seed(1) a = random.sample(a,10) print a then change the sample much higher and the sequence will change(at least for me they always do): a = random.sample(a,40)

python seed() not keeping same sequence

依然范特西╮ 提交于 2020-05-27 03:07:11
问题 I'm using a random.seed() to try and keep the random.sample() the same as I sample more values from a list and at some point the numbers change.....where I thought the one purpose of the seed() function was to keep the numbers the same. Heres a test I did to prove it doesn't keep the same numbers. import random a=range(0,100) random.seed(1) a = random.sample(a,10) print a then change the sample much higher and the sequence will change(at least for me they always do): a = random.sample(a,40)

Randomizing a list in Python [duplicate]

旧巷老猫 提交于 2020-05-24 21:32:25
问题 This question already has answers here : Shuffling a list of objects (23 answers) Closed 4 years ago . I am wondering if there is a good way to "shake up" a list of items in Python. For example [1,2,3,4,5] might get shaken up / randomized to [3,1,4,2,5] (any ordering equally likely). 回答1: from random import shuffle list1 = [1,2,3,4,5] shuffle(list1) print list1 ---> [3, 1, 2, 4, 5] 回答2: Use random.shuffle : >>> import random >>> l = [1,2,3,4] >>> random.shuffle(l) >>> l [3, 2, 4, 1] random

How do I generate random numbers in Dart?

二次信任 提交于 2020-05-24 09:23:12
问题 How do I generate random numbers using Dart? 回答1: Use Random class from dart:math : import 'dart:math'; main() { var rng = new Random(); for (var i = 0; i < 10; i++) { print(rng.nextInt(100)); } } This code was tested with the Dart VM and dart2js, as of the time of this writing. 回答2: If you need cryptographically-secure random numbers (e.g. for encryption), and you're in a browser, you can use the DOM cryptography API: int random() { final ary = new Int32Array(1); window.crypto

How to assign random values from a list to a column in a pandas dataframe?

a 夏天 提交于 2020-05-23 13:12:11
问题 I am working with Python in Bigquery and have a large dataframe df (circa 7m rows). I also have a list lst that holds some dates (say all days in a given month). I am trying to create an additional column "random_day" in df with a random value from lst in each row. I tried running a loop and apply function but being quite a large dataset it is proving challenging. My attempts passed by the loop solution: df["rand_day"] = "" for i in a["row_nr"]: rand_day = sample(day_list,1)[0] df.loc[i,"rand

what is the difference between uuid4 and secrets token_bytes in python?

不羁岁月 提交于 2020-05-17 06:48:06
问题 Checked the cpython source code for both secrets and uuid4. Both seems to be using os.urandom. #uuid.py def uuid4(): """Generate a random UUID.""" return UUID(bytes=os.urandom(16), version=4) #secrets.py def token_bytes(nbytes=None): """Return a random byte string containing *nbytes* bytes. If *nbytes* is ``None`` or not supplied, a reasonable default is used. >>> token_bytes(16) #doctest:+SKIP b'\\xebr\\x17D*t\\xae\\xd4\\xe3S\\xb6\\xe2\\xebP1\\x8b' """ if nbytes is None: nbytes = DEFAULT

Select randomly x files in subdirectories

穿精又带淫゛_ 提交于 2020-05-16 18:19:13
问题 I need to take exactly 10 files (images) in a dataset randomly, but this dataset is hierarchically structured. So I need that for each subdirectory that contains images hold just 10 of them randomly. Is there an easy way to do that or I should do it manually? def getListOfFiles(dirName): ### create a list of file and sub directories ### names in the given directory listOfFile = os.listdir(dirName) allFiles = list() ### Iterate over all the entries for entry in listOfFile: ### Create full path

Select randomly x files in subdirectories

筅森魡賤 提交于 2020-05-16 18:19:12
问题 I need to take exactly 10 files (images) in a dataset randomly, but this dataset is hierarchically structured. So I need that for each subdirectory that contains images hold just 10 of them randomly. Is there an easy way to do that or I should do it manually? def getListOfFiles(dirName): ### create a list of file and sub directories ### names in the given directory listOfFile = os.listdir(dirName) allFiles = list() ### Iterate over all the entries for entry in listOfFile: ### Create full path