random

java add items to arraylist at random time intervals

点点圈 提交于 2020-01-15 07:47:12
问题 trying to add an item at random time intervals. I was thinking I need to start when i is equal to arriveTime, once that is met I need to create a new random arrival and add i (otherwise it will not happen again as i is already past arrival. So I add another if, once that is met create new arrival time and add i again. pseudocode seems to make sense, code not so much, any help is appreciated. ArrayList<Integer> q = new ArrayList<Integer>(); Random r = new Random(); int arrivals = 0; for (int i

Why to add 0 while using nextInt()

荒凉一梦 提交于 2020-01-15 06:46:14
问题 I am following an instructor creating tic tac toy game, to make it autoplay. var r = Random() val randInt = r.nextInt(emptyCell.size-0) + 0 // adding 0 here why do we need to add +0 here? 回答1: There's no reason why you'd have to write down + 0 in that case. nextInt returns an Int , so adding 0 as an Int to it does absolutely nothing - doesn't change the type or affect the value - as you'd expect. Probably a typo in the tutorial. 回答2: It's a billet for changing a value if you wish to. Author

Multiple random values in SQL Server 2005

爱⌒轻易说出口 提交于 2020-01-15 05:55:08
问题 I need to generate multiple random values under SQL Server 2005 and somehow this simply wont work with Random(Value) as ( select rand() Value union all select rand() from Random )select top 10 * from Random Whats the preffered workaround? 回答1: have you tries something like this (found at http://weblogs.sqlteam.com ) : CREATE VIEW vRandNumber AS SELECT RAND() as RandNumber GO create a function CREATE FUNCTION RandNumber() RETURNS float AS BEGIN RETURN (SELECT RandNumber FROM vRandNumber) END

How to select random lines from a file

折月煮酒 提交于 2020-01-15 05:52:29
问题 I have a text file containing 10 hundreds of lines, with different lengths. Now I want to select N lines randomly, save them in another file, and remove them from the original file. I've found some answers to this question, but most of them use a simple idea: sort the file and select first or last N lines. unfortunately this idea doesn't work to me, because I want to preserve the order of lines. I tried this piece of code, but it's very slow and takes hours. FILEsrc=$1; FILEtrg=$2; MaxLines=

Bimodal distribution in C or Python

自闭症网瘾萝莉.ら 提交于 2020-01-15 03:15:06
问题 What's the easiest way to generate random values according to a bimodal distribution in C or Python? I could implement something like the Ziggurat algorithm or a Box-Muller transform, but if there's a ready-to-use library, or a simpler algorithm I don't know about, that'd be better. 回答1: Aren't you just picking values either of two modal distributions? http://docs.python.org/library/random.html#random.triangular Sounds like you just toggle back and forth between two sets of parameters for

Is rapidly creating BouncyCastle SecureRandom instances problematic?

半城伤御伤魂 提交于 2020-01-15 02:29:21
问题 As noted at Random number generator only generating one random number, it's generally incorrect to create a new instance of System.Random every time that you need another random number, since System.Random is seeded based upon the clock and so multiple instances created in the same tick will yield identical random numbers. As such, one common practice (at least in single-threaded applications) is to create a single instance of Random stored in a static field that is used for all random number

Random number generator without replacement?

左心房为你撑大大i 提交于 2020-01-15 01:06:55
问题 I have tried to program a random number generator that doesn't generate the same random number more than once . But I am unable to and can't figure out why. My code is like this at the moment: public void printNS(){ System.out.print("Numeros Numeros: "); for(int i=0; i < 5 ; i++){ System.out.print( (int)(Math.random()*50) + ","); } System.out.print("; Numeros Stars: "); for(int i=0; i < 2 ; i++){ System.out.print( (int)(Math.random()*12)+ ","); } } 回答1: in java 8 you can do the following int[

Random number generator without replacement?

有些话、适合烂在心里 提交于 2020-01-15 01:06:29
问题 I have tried to program a random number generator that doesn't generate the same random number more than once . But I am unable to and can't figure out why. My code is like this at the moment: public void printNS(){ System.out.print("Numeros Numeros: "); for(int i=0; i < 5 ; i++){ System.out.print( (int)(Math.random()*50) + ","); } System.out.print("; Numeros Stars: "); for(int i=0; i < 2 ; i++){ System.out.print( (int)(Math.random()*12)+ ","); } } 回答1: in java 8 you can do the following int[

Why isn't this Random number generation code working?

最后都变了- 提交于 2020-01-14 19:22:07
问题 I'm writing a program for proving the 'Birthday Paradox'. For i = 0 To (pnum - 1) days(i) = rnd(h:=365) Next It generates a random number for every i (days(i)) between 1 and 365, the function is: Private Function rnd(h As Integer) Dim num As Integer Dim rnum As Random rnum = New Random num = rnum.Next(1, h) Return num End Function When I add a breakpoint in the for loop and go through it manually, it works fine, but if I just run the program, it puts the same random number in every slot in

How do I display a random phrase from a list when a button is clicked in a web page?

僤鯓⒐⒋嵵緔 提交于 2020-01-14 19:22:07
问题 I am creating a web page where someone can visit it. They type a question in a field and click a button, and a response is passed back to them. (Kind of like a magic 8 ball). What I'm trying to do is set it up something like this: http://img585.imageshack.us/img585/997/layoutoi.png I'm still new to hand-coding things - I have a book on HTML/CSS and one on PHP, which lay still unread, so I'll probably need a step-by-step process. (I have a host and everything, so that's already taken care of.)