random

嵩天老师用蒙特卡罗方法和python求圆周率详细代码解析

↘锁芯ラ 提交于 2020-01-02 16:08:31
from time import perf_counter from random import random start=perf_counter() x=10 sum=0 while x>0: zheng= 10000*10000 #相当于正方形的面积 yuan=0 #相当于圆的面积 for i in range(1,zheng): a,b=random(),random() r=pow(a**2+b**2,0.5) #用勾股定理求到圆心的距离(圆的半径) if r<=1: yuan = yuan+1 pi=4*(yuan/zheng) x=x-1 sum = sum +pi print('10次平均值:π={:.7f}'.format(sum/10)) print('亿次循环耗时:{:.2f}秒'.format(perf_counter()-start)) 来源: CSDN 作者: qqfushi 链接: https://blog.csdn.net/qqfushi/article/details/103805000

Non-repeating random numbers inside array JAVA

一个人想着一个人 提交于 2020-01-02 15:52:11
问题 I would like to generate 6 numbers inside an array and at the same time, having it compared so it will not be the same or no repeating numbers. For example, I want to generate 1-2-3-4-5-6 in any order, and most importantly without repeating. So what I thought is to compare current array in generated array one by one and if the number repeats, it will re-run the method and randomize a number again so it will avoid repeating of numbers. Here is my code: import javax.swing.*; public class

Creating a Function in SQL Server with a Phone Number as a parameter and returns a Random Number

允我心安 提交于 2020-01-02 09:41:43
问题 I am hoping someone can help me here as google is not being as forthcoming as I would have liked. I am relatively new to SQL Server and so this is the first function I have set myself to do. The outline of the function is that it has a Phone number varchar(15) as a parameter, it checks that this number is a proper number, i.e. it is 8 digits long and contains only numbers. The main character I am trying to avoid is '+'. Good Number = 12345678 Bad Number = +12345678. Once the number is checked

Get a random sample of a dict

耗尽温柔 提交于 2020-01-02 07:26:13
问题 I'm working with a big dictionary and for some reason I also need to work on small random samples from that dictionary. How can I get this small sample (for example of length 2)? Here is a toy-model: dy={'a':1, 'b':2, 'c':3, 'd':4, 'e':5} I need to perform some task on dy which involves all the entries. Let us say, to simplify, I need to sum together all the values: s=0 for key in dy.key: s=s+dy[key] Now, I also need to perform the same task on a random sample of dy; for that I need a random

Setting a button's position to random points on screen? - Android

大憨熊 提交于 2020-01-02 07:15:19
问题 Below, I've got both the Java and XML code, basically I'm trying to click a fixed button, and make the 2nd button jump around the screen at random points XML Explanation: So, I've got a relative layout with two buttons... One of them is fixed at the bottom of the screen... and I want to set the other one's position randomly. Screen opens, Click the fixed button... and the other button's position should change randomly. Say I click the fixed button again and agin, the other button should jump

Setting a button's position to random points on screen? - Android

谁说胖子不能爱 提交于 2020-01-02 07:15:09
问题 Below, I've got both the Java and XML code, basically I'm trying to click a fixed button, and make the 2nd button jump around the screen at random points XML Explanation: So, I've got a relative layout with two buttons... One of them is fixed at the bottom of the screen... and I want to set the other one's position randomly. Screen opens, Click the fixed button... and the other button's position should change randomly. Say I click the fixed button again and agin, the other button should jump

Generate list of n (strictly positive) values such that the list has predetermined mean x and std. dev. y

梦想与她 提交于 2020-01-02 07:14:00
问题 I would like to generate a list of n strictly positive values such that the list has a predetermined mean and standard deviation (can be close/not exact). I was using the uniform distribution's equations for expectation and variance and solving for 'a' and 'b', but the system of equations (for the specific mean and std. dev. I wanted) had no solutions for a, b >= 0. I was wondering if there was a plug-and-chug method to do this in any programming language, but hopefully in Python. Thanks! Ex:

Weird behaviour when seeding Java Random

旧巷老猫 提交于 2020-01-02 07:01:18
问题 The following code should create two Random objects with identical seeds: System.out.println("System time before: " + System.currentTimeMillis()); Random r1 = new Random(); Random r2 = new Random(System.currentTimeMillis()); System.out.println("System time after: " + System.currentTimeMillis()); System.out.println("r1: " + r1.nextInt()); System.out.println("r2: " + r2.nextInt()); The seeds should be identical since System.currentTimeMillis() did not change before and after creating the two

Weird behaviour when seeding Java Random

荒凉一梦 提交于 2020-01-02 07:01:06
问题 The following code should create two Random objects with identical seeds: System.out.println("System time before: " + System.currentTimeMillis()); Random r1 = new Random(); Random r2 = new Random(System.currentTimeMillis()); System.out.println("System time after: " + System.currentTimeMillis()); System.out.println("r1: " + r1.nextInt()); System.out.println("r2: " + r2.nextInt()); The seeds should be identical since System.currentTimeMillis() did not change before and after creating the two

Implementation of random number generator [duplicate]

此生再无相见时 提交于 2020-01-02 06:45:44
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How does a random number generator work? I am looking for internal implementation of a random number generator in C/C++.Basically I am interested to know, what exactly happens when rand() is called. After all machine follows definite set of instructions, how can it be random! Edit: Want to know how can I implement one in C/C++. 回答1: They're pseudo-random number generators, not truly random ones. This is often a