random

Math#random ain't so random?

試著忘記壹切 提交于 2020-01-03 13:34:00
问题 I am finding something odd happening with my program. This program basically is used for component clicking, as it is a concept to test randomness. As you can see, it is printing correctly, as it should have a tendency to click towards the middle, which is does perfect. Problem is, it seems biased. import java.applet.Applet; import java.awt.Point; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.awt

Randomly initialize BitSet in JAVA

半世苍凉 提交于 2020-01-03 12:29:09
问题 I have BitSet which has to be initialized randomly. Is there any method to do that? Thanks in advance. 回答1: Just go through BitSet and call nextBoolean() of the Random class. 回答2: If you are using Java 7, you can initialize a random byte array with Random.nextBytes(byte[]) then use the static BitSet.valueOf(byte[]) method to create a BitSet from the same byte array. Random rnd = new Random(); // ... byte[] randomBytes = new byte[NUM_BYTES]; rnd.nextBytes(randomBytes); return BitSet.valueOf

Why is my mt19937 Random generator giving me ridiculous results? C++

自作多情 提交于 2020-01-03 10:30:06
问题 Working on another project and we're required to use the mt19937 for randomly generating numbers. We're supposed to have it randomly choose an x and y coordinate based on the section of a grid. For example, my function passes minX , maxX , minY , maxY to a function. My x coordinate works fine. I kept getting errors randomly upon test runs. Sometimes it'll run 10 times with no problem, then hits an error. I put in some self debug lines to display what the mt generator is actually generating.

shell实现随机数多种方法(data,random,uuid)

自闭症网瘾萝莉.ら 提交于 2020-01-03 09:24:54
在日常生活中,随机数实际上经常遇到,想丢骰子,抓阄,还有抽签。呵呵,非常简单就可以实现。那么在做程序设计,真的要通过自己程序设计出随机数那还真的 不简单了。现在很多都是操作系统内核会提供相应的api,这些原始参数是获取一些计算机运行原始信息,如内存,电压,物理信号等等,它的值在一个时间段可 以保证是唯一的了。好了,废话我就不说了。呵呵。 shell脚本程序我们有那些获得随机数方法呢? 一、通过时间获得随机数(date) 这个也是我们经常用到的,可以说时间是唯一的,也不会重复的,从这个里面获得同一时间的唯一值。适应所有程序里面了。 例子: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 [chengmo@centos5 shell]$ date +%s 1287764773 #获得时间戳,当前到:1970-01-01 00:00:00 相隔的秒数 #如果用它做随机数,相同一秒的数据是一样的。在做循环处理,多线程里面基本不能满足要求了。 [chengmo@centos5 shell]$ date +%N 738710457 #获得当前时间的纳秒数据,精确到亿分之一秒。 #这个相当精确了,就算在多cpu,大量循环里面,同一秒里面,也很难出现相同结果,不过不同时间里面还会有大量重复碰撞 [chengmo@centos5 shell]$ date +%s%N

Can I randomly sample from a HashSet efficiently?

ぐ巨炮叔叔 提交于 2020-01-03 09:05:10
问题 I have a std::collections::HashSet , and I want to sample and remove a uniformly random element. Currently, what I'm doing is randomly sampling an index using rand.gen_range , then iterating over the HashSet to that index to get the element. Then I remove the selected element. This works, but it's not efficient. Is there an efficient way to do randomly sample an element? Here's a stripped down version of what my code looks like: use std::collections::HashSet; extern crate rand; use rand:

random 随机数生成

北战南征 提交于 2020-01-03 08:54:00
栗子: # 题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。 # 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后此元素之后的数,依次后移一个位置。 import random list =[random.randint(0,100) for _ in range(10)] b=37 #要插入的数 print(list) #数应该先排序 def bubbleSort(arr): for i in range(1, len(arr)): for j in range(0, len(arr)-i): if arr[j] > arr[j+1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] return arr print(bubbleSort(list)) list.append(b) list.sort() print(list) random 每次只能随机生成一个数,要是和上述一样生成一个列表;使用列表生成式; list =[random.randint(0,100) for _ in range(10)] random 随机数生成 Python中的random模块用于生成随机数。下面介绍一下random模块中最常用的几个函数。 random.random random.random() 

Groovy method to get a random element from a list

拜拜、爱过 提交于 2020-01-03 08:53:24
问题 Groovy is extremely powerful managing collections. I have a list like this one: def nameList = ["Jon", "Mike", "Alexia"] What I am trying to do is iterating 10 times to get ten people with a random name from the first list. 10.times{ Person person = new Person( name: nameList.get() //I WANT TO GET A RANDOM NAME FROM THE LIST ) } This is not working for two obvious reasons, I am not adding any index in my nameList.get and I am not creating 10 different Person objects. How can I get a random

Groovy method to get a random element from a list

痴心易碎 提交于 2020-01-03 08:52:56
问题 Groovy is extremely powerful managing collections. I have a list like this one: def nameList = ["Jon", "Mike", "Alexia"] What I am trying to do is iterating 10 times to get ten people with a random name from the first list. 10.times{ Person person = new Person( name: nameList.get() //I WANT TO GET A RANDOM NAME FROM THE LIST ) } This is not working for two obvious reasons, I am not adding any index in my nameList.get and I am not creating 10 different Person objects. How can I get a random

Groovy method to get a random element from a list

我们两清 提交于 2020-01-03 08:52:23
问题 Groovy is extremely powerful managing collections. I have a list like this one: def nameList = ["Jon", "Mike", "Alexia"] What I am trying to do is iterating 10 times to get ten people with a random name from the first list. 10.times{ Person person = new Person( name: nameList.get() //I WANT TO GET A RANDOM NAME FROM THE LIST ) } This is not working for two obvious reasons, I am not adding any index in my nameList.get and I am not creating 10 different Person objects. How can I get a random

Ruby: Using rand() in code but writing tests to verify probabilities

天涯浪子 提交于 2020-01-03 08:50:14
问题 I have some code which delivers things based on weighted random. Things with more weight are more likely to be randomly chosen. Now being a good rubyist I of couse want to cover all this code with tests. And I want to test that things are getting fetched according the correct probabilities. So how do I test this? Creating tests for something that should be random make it very hard to compare actual vs expected. A few ideas I have, and why they wont work great: Stub Kernel.rand in my tests to