c# - Problems in generating random colors -
i want generate random colors in wpf , store them in array.
random r; color[] colarr = new color[6]; (int = 0; < colarr.length; i++) { color c=new color(); r=new random(); r.next(); c.r = (byte)r.next(1, 255); c.g = (byte)r.next(1, 255); c.b = (byte)r.next(1, 255); c.a = (byte)r.next(1, 255); c.b = (byte)r.next(1, 255); colarr[i] = c; }
but elements of array represent 1 single color. when debugged code, found random colors every element, when code executed(not in debug mode) same color generated. makes clear code correct, there problem while execution.
edit :
how can increase randomness of colors generated?
the problem making new instance of random each run. should set once.
the default seed random system time, same if repeat fast loop; time won't change lot. if set @ start, random work expected.
i suggest use r.next(0, 256)
, since give value ranging 0 255.
also, call r.next()
after definition of color c
unnecessary, since don't use value.
random r = new random(); color[] colarr = new color[6]; (int = 0; < colarr.length; i++) { color c=new color(); c.r = (byte)r.next(0, 256); c.g = (byte)r.next(0, 256); c.b = (byte)r.next(0, 256); c.a = (byte)r.next(0, 256); //c.b = (byte)r.next(1, 255); line isn't needed btw colarr[i] = c; }
Comments
Post a Comment