java - Random - nextInt(int y) isn't able to give me 18 even integers in a row when 'int y' % 2 == 0 && 'int y' != a power of 2 -
sorry title wanted pack information problem in little space possible without being confusing.
so, have loop runs n times , each time uses = r.nextint(int y); generate int , if n integers generated numbers, program "returns true".
the weird thing is: if chose n 18 or higher while y , number not power of 2, programm not "termintate successfully".
i love me, , can take heavy dose of criticism. (i know i'm asking random/nextint(int) topic take tips better coding)
edit: looked documentation java8 befor posted here , powers of 2 method uses different way of producing random number.
what don't understand why 18 breakpoint consecutive numbers , why work odd numbers nextint(int)?
so following code work howmanyints = 16
or 17
not 18 (or higher)
when nextintvalue
number not power of 2 (6,10,12...)
it works howmanyints = 25 , nextintvalue = 8 in less 20 seconds
import java.util.*; class test{ public static void main(string[] args) { boolean win = false; int areeven = 0; long loopcounter = 0; // loopcounter used control maximum number of loops should run incase loop endless int howmanyints = 18; int nextintvalue = 6; // nextintvalue = 6 or 10 won't work while powers of 2 work fine // also, don't want odd value change odds towards odd values... while(win == false){ loopcounter += 1; areeven = 0; random r = new random(); int[] num = new int[howmanyints]; for(int = 0; < num.length; a++){ num[a] = r.nextint(nextintvalue); if(num[a] % 2 == 0){ areeven += 1; } } if(areeven == num.length || loopcounter >= 10000000){ win = true; system.out.println("it took " + loopcounter + " loops " + num.length + " random values even."); } } } }
if use securerandom
instead of random
, program finish quickly.
another way use nextdouble
instead
num[a] = (int) (r.nextdouble() * nextintvalue);
the problem random.nextint(int n)
believe hidden within implementation , can read in javadoc.
the algorithm tricky. rejects values result in uneven distribution (due fact 2^31 not divisible n). probability of value being rejected depends on n. worst case n=2^30+1, probability of reject 1/2, , expected number of iterations before loop terminates 2.
algorithm treats case n power of 2 specially: returns correct number of high-order bits underlying pseudo-random number generator. in absence of special treatment, correct number of low-order bits returned. linear congruential pseudo-random number generators such 1 implemented class known have short periods in sequence of values of low-order bits. thus, special case increases length of sequence of values returned successive calls method if n small power of two.
the implementation looks this:
public int nextint(int n) { if (n <= 0) throw new illegalargumentexception("n must positive"); if ((n & -n) == n) // i.e., n power of 2 return (int)((n * (long)next(31)) >> 31); int bits, val; { bits = next(31); val = bits % n; } while (bits - val + (n-1) < 0); return val; }
while next
method looks (i've replaced constants literals)
protected int next(int bits) { long oldseed, nextseed; atomiclong seed = this.seed; { oldseed = seed.get(); nextseed = (oldseed * 0x5deece66dl + 0xbl) & ((1l << 48) - 1); } while (!seed.compareandset(oldseed, nextseed)); return (int)(nextseed >>> (48 - bits)); }
(i suppose 48-31 == 17
purely coincidental)
Comments
Post a Comment