for-loop

How to plot the Monte Carlo pi histogram?

旧城冷巷雨未停 提交于 2021-01-29 16:12:47
问题 I am trying to plot a histogram distribution of pi from the Monte Carlo method but I am getting histograms that are either skewed to the left or right each time I run the simulation instead of a histogram that is approximately symmetric and peaks at around 3.14. The output histograms also have some gaps and I think I am approximating pi correctly. My code is below: [...(importing relevant modules)] N = 1000 #total number of random points circlex = [] circley = [] squarex = [] squarey = [] pis

How to pause javascript for loop (animating bubble sort)? [duplicate]

China☆狼群 提交于 2021-01-29 16:11:19
问题 This question already has answers here : How do I add a delay in a JavaScript loop? (31 answers) Closed 8 months ago . I am new to javascript so sorry if I am misunderstanding how the language does some stuff, I am building a sorting algorithms visualizer which orders blocks by their hue value (using chroma-js library) : Each item in screenObject.items is a Color object //color objects are what I am sorting class Color { constructor(div, color, value) { //this div on the html page this.div =

Python for loop posting json to url?

浪尽此生 提交于 2021-01-29 16:10:46
问题 The following code works but only uploads the one result at the index as per the below, see ['result'][2]["text"] - which uploads the 'text' result at index 2 perfectly: with open("test.json") as json_file: data = json.load(json_file) connection.request('POST', '/1/batch', json.dumps({ "requests": [{ "method": "POST", "path": "/1/classes/test", "body": { "playerName": data['results'][2]["text"], "Url": data['results'][2]["Url"], "Amount": data['results'][2]["Amount"] } }] }) How can I loop

How to fix the list of output from for-loop always store the last value of last index in python

那年仲夏 提交于 2021-01-29 14:29:40
问题 I try to store the number of weekofyear from the list of data named date_range which stored the data such as date_range[1] = DatetimeIndex(['2020-03-02', '2020-03-03', '2020-03-04', '2020-03-05', '2020-03-06', '2020-03-07', '2020-03-08', '2020-03-09', '2020-03-10', '2020-03-11', '2020-03-12', '2020-03-13', '2020-03-14', '2020-03-15', '2020-03-16', '2020-03-17', '2020-03-18', '2020-03-19', '2020-03-20', '2020-03-21', '2020-03-22', '2020-03-23', '2020-03-24', '2020-03-25', '2020-03-26', '2020

Python For Loop Slows Due To Large List

戏子无情 提交于 2021-01-29 12:52:10
问题 So currently I have a for loop, which causes the python program to die with the program saying 'Killed'. It slows down around 6000 items in, with the program slowly dying at around 6852 list items. How do I fix this? I assume it's due to the list being too large. I've tried splitting the list in two around 6000. Maybe it's due to memory management or something. Help would be appreciated. for id in listofids: connection = psycopg2.connect(user = "username", password = "password", host =

MATLAB: Perform “For-loop or IF-statement” only for specific character arrays

[亡魂溺海] 提交于 2021-01-29 11:20:24
问题 I have 60 different character arrays (Book01, Book02, ..., Book60). (For example Book01 is a 1x202040 char.). I want to do a certain procedure only on Book45 until Book58. How do I write an IF-statement or FOR-loop, so that the procedure is only performed for character arrays Book45 until Book58? For example: Book05 % Inserted Array for test if Book45|Book46|Book47|Book48|Book49|Book50|Book51|Book52|Book53|Book54|Book54|Book56|Book57|Book58 % If inserted array is Book45-58 % Procedure to be

Loop to list files in parts

╄→гoц情女王★ 提交于 2021-01-29 10:29:33
问题 I'm trying to copy around 10,000 files and paste it into another directory with the following code: f <- c (MYFILE $ COL1) # PART 1 m <- paste0 ("(", paste (f, collapse = "|"), ") .xml") #PART 2 files <- list.files (pattern = m) #PART 3 file.copy (paste0 (getwd (), "/", files), paste0 (getwd (), "/ FILES_XML /", files), overwrite = TRUE) # PART 4 When I try to run the files <- list.files (pattern = m) part, the following message appears: assertion 'tree-> num_tags == num_tags' failed in

Creating pointers inside a for loop results in pointing to same memory address

烈酒焚心 提交于 2021-01-29 10:05:14
问题 I have an object that I am trying to duplicate for any number of times with some minor changes to that object. I want to store pointers to those duplicated objects in a std::vector . I am using a for loop to try and achieve the result. However, what I notice is that the std::vector<T *> is pointing to the same address after the loop exits. I tried this attempt to duplicate objects with std::string and I see the same effects. Here's my code snippet. int main() { auto name = new std::string(

Is there an easy way to simplify this code using a loop?

最后都变了- 提交于 2021-01-29 09:50:44
问题 Is there a way to simplify this code using a loop? set.seed(100) AL_INDEX <- sample(1:nrow(AL_DF), 0.7*nrow(AL_DF)) AL_TRAIN <- AL_DF[AL_INDEX,] AL_TEST <- AL_DF[-AL_INDEX,] AR_INDEX <- sample(1:nrow(AR_DF), 0.7*nrow(AR_DF)) AR_TRAIN <- AR_DF[AR_INDEX,] AR_TEST <- AR_DF[-AR_INDEX,] AZ_INDEX <- sample(1:nrow(AZ_DF), 0.7*nrow(AZ_DF)) AZ_TRAIN <- AZ_DF[AZ_INDEX,] AZ_TEST <- AZ_DF[-AZ_INDEX,] AL_DF, AR_DF & AZ_DF are data frames that have the same field structure, but different number of records.

Run a Monte Carlo for each row of a data frame in R

陌路散爱 提交于 2021-01-29 09:30:15
问题 I am trying to run a Monte Carlo for each row of a data frame using R. library(tidyverse) Group.ID <- c('A','B','C') Start.Amount <- c(90.2, 11.7, 37.8) Mean <- c(0.0005106365,0.0006589744,0.000444903) SD <- c(0.01587259,0.02358892,0.02070972) summary <- data.frame(Group.ID, Start.Amount, Mean, SD) For instance I am trying to run a Monte Carlo for Group.ID A then run a different simulation for Group.ID B and so on. I am running a single Group.ID with the following code. #### Monte Carlo