|
Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks. |
okay, we were discussing this in class but something bugged me.
Assume
0 <= n < 1 0 < p <= 1
n is your RNG p is the probability.
which is better
if (n < p) return true else false
OR
if (n<=p) return true else false
is there a difference? does it affect the probability at all? which one would you choose?
sorry if its a stupid question/topic. its one of the things that wont let me sleep at night.
|
On July 02 2013 18:11 icystorage wrote: okay, we were discussing this in class but something bugged me.
Assume
0 <= n < 1 0 < p <= 1
n is your RNG p is the probability.
which is better
if (n < p) return true else false
OR
if (n<=p) return true else false
is there a difference? does it affect the probability at all? which one would you choose?
sorry if its a stupid question/topic. its one of the things that wont let me sleep at night.
It depends on the requirements and usecase. No matter which values for n and p you pick, (n <= p) has a higher probability of being true since both numbers can be equal and don't depend on each other. There is no case where (n < p) would be true while (n <= p) would be false but there are cases where the opposite is true.
There is no correct way to chose since it depends on what your application is supposed to do.
|
|
|
On July 02 2013 18:11 icystorage wrote: okay, we were discussing this in class but something bugged me.
Assume
0 <= n < 1 0 < p <= 1
n is your RNG p is the probability.
which is better
if (n < p) return true else false
OR
if (n<=p) return true else false
is there a difference? does it affect the probability at all? which one would you choose?
sorry if its a stupid question/topic. its one of the things that wont let me sleep at night.
In a mathematical sense, both solutions are equal as the event of n = p has a likelihood of 0 in the real numbers.
But reals on a computer don't come with arbitrarily large precision, so there is a finite, but very small, chance that n = p, in which case the second expression would return true, where the first doesn't. The exact probability depends on the precision of the floating point variables used for n and p. A regular 64-bit double has 52 bits of data for the fraction, which makes the probability of hitting n = p equal to 1 : 2^52.
(edit: assuming the PRNG provides a uniform distribution for all 52 bits)
With this difference, for pretty much every practical application, there is no difference between the two expressions.
|
Performance-wise, i don't think there's significant impact beween those two - looking on x86 assembly, 'jle' and 'jl' instructions take same time ( according to Intel Instruction Set Reference )
|
On July 02 2013 18:58 beamerkun wrote: Performance-wise, i don't think there's significant impact beween those two - looking on x86 assembly, 'jle' and 'jl' instructions take same time ( according to Intel Instruction Set Reference ) This. While it depends on the implementation of the language at hand, in pure assembly it shouldn't matter, so assuming the language implementation is effective enough, the performance should be identical.
|
ooh okay thanks! the more you know 
btw, i hope you guys took into consideration the range of n and p
|
On July 02 2013 20:20 icystorage wrote:ooh okay thanks! the more you know  btw, i hope you guys took into consideration the range of n and p
The range of n and p has no influence on this.
(n <= p) still includes all the cases for which (n < p) is true but also cases for which (n < p) would be false. The rest depends on the precision of n and p. As Rannasha said, if you use the full range of a double, they are virtually identical since the chance of getting identical doubles is tiny. If you use a smaller precision, e.g. by using something like "n = (double)randomInt / 100.0" then they behave differently due to the higher chance of n == p.
|
On July 02 2013 20:37 Morfildur wrote:Show nested quote +On July 02 2013 20:20 icystorage wrote:ooh okay thanks! the more you know  btw, i hope you guys took into consideration the range of n and p The range of n and p has no influence on this. (n <= p) still includes all the cases for which (n < p) is true but also cases for which (n < p) would be false. The rest depends on the precision of n and p. As Rannasha said, if you use the full range of a double, they are virtually identical since the chance of getting identical doubles is tiny. If you use a smaller precision, e.g. by using something like "n = (double)randomInt / 100.0" then they behave differently due to the higher chance of n == p. that explains it, we used double for n and p thanks!
|
On July 02 2013 20:37 Morfildur wrote:Show nested quote +On July 02 2013 20:20 icystorage wrote:ooh okay thanks! the more you know  btw, i hope you guys took into consideration the range of n and p The range of n and p has no influence on this. (n <= p) still includes all the cases for which (n < p) is true but also cases for which (n < p) would be false. The rest depends on the precision of n and p. As Rannasha said, if you use the full range of a double, they are virtually identical since the chance of getting identical doubles is tiny. If you use a smaller precision, e.g. by using something like "n = (double)randomInt / 100.0" then they behave differently due to the higher chance of n == p.
The only thing is, that you don't allow p == 0, which seems pretty arbitrary given that you allow p == 1. Why allow 100% probability, but not 0%?
If you do allow p == 0, you have to reconsider which comparison to use (you can't use n <= p because you want p == 0 and n == 0 to return false).
|
On July 02 2013 09:54 ferdkuh wrote:It is much faster if you take a screen capture of the area you're interested in and then search the pixel data of the image. BufferedImage image = screen.createScreenCapture(new Rectangle(X_MIN, Y_MIN, X_MAX, Y_MAX)) Array[int] pixelColors = ((DataBufferInt) image.getRaster.getDataBuffer).getData
Then simpyl iteratore over pixelColors and compare to YOURCOLOR.getRGB Thanks, ive used that and googled how to implement it and came to this http://stackoverflow.com/questions/6524196/java-get-pixel-array-from-image However that doesnt really work as expected. First, i dont get why he increments the pixel counter when assembling the array
argb += -16777216; // 255 alpha argb += ((int) pixels[pixel] & 0xff); // blue argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red
Because of this it only works when the array is a multitude of 3, otherwise i get an ArrayIndexOutofBounds exception, for obvious reasons. And then it only seems to find the pixel, if the color is 3x the same value, eg 255,255,255, but not 255,255,254. Whats wrong there? Relevant code: + Show Spoiler + private static int[][] convertTo2DWithoutUsingGetRGB(BufferedImage image) {
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); final int width = image.getWidth(); final int height = image.getHeight(); final boolean hasAlphaChannel = image.getAlphaRaster() != null;
int[][] result = new int[height][width]; if (hasAlphaChannel) { final int pixelLength = 4; for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) { int argb = 0; argb += (((int) pixels[pixel] & 0xff) << 24); // alpha argb += ((int) pixels[pixel + 1] & 0xff); // blue argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red result[row][col] = argb; col++; if (col == width) { col = 0; row++; } } } else { final int pixelLength = 3; for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) { int argb = 0; argb += -16777216; // 255 alpha argb += ((int) pixels[pixel] & 0xff); // blue argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red result[row][col] = argb; col++; if (col == width) { col = 0; row++; } } }
return result; }
I also had to change DataBufferByte to DataBufferInt in
final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData(); Otherwise id get Exception in thread "main" java.lang.ClassCastException: java.awt.image.DataBufferInt cannot be cast to java.awt.image.DataBufferByte
Edit: i think i found my problem. Using DataBufferInt results in the pixel array only having the int value, so the array is only width*height long and i dont have to manually assemble the int first, and dont have to care about alpha because its a screencapture.
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel ++) { result[col][row] = pixels[pixel]; col++; if (col == width) { col = 0; row++; } }
This seems to work now.
|
On July 02 2013 04:16 HyunA wrote:Can anybody please explain to me how does compareTo work in java for strings? I can't seem to wrap my head around it :/ Like: System.out.print("Comparing \"axe\" with \"dog\" produces "); int i = "axe".compareTo("dog"); System.out.println(i);The output is -3. But what does it actually compare?
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#compareTo(java.lang.String)
The implementation details are there. More specifically, what -3 means is that axe is smaller than (which could be interpreted as "comes before") dog. Basically when you call a.compareTo(b), if a > b, it's after, a < b, it's before and a == b it's the same. (which is also the check you should make - the integer in itself doesn't carry any meaning)
|
Anyone else using Visual Studio 2012 Update 3 (more specifically, for C++-programming) on 64-bit Windows 7 that has problems that it freezes frequently (and at times, Windows all completely)? Today, it has happened for me a couple of times already, and it's getting really annoying. Now, as a result, I have to set up a new project because it caused corrupted files. It's not the code files that are the problem, those are on my Dropbox and I can revert should I need it, It's the project itself.
Before the most recent freeze, it said some error message about that my graphics card driver has crashed (I don't know what, I didn't manage to screenshot it). I'm running a Nvidia GeForce 555M with driver 320.49
|
On July 03 2013 00:37 Warri wrote:First, i dont get why he increments the pixel counter when assembling the array argb += -16777216; // 255 alpha argb += ((int) pixels[pixel] & 0xff); // blue argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red
Because of this it only works when the array is a multitude of 3, otherwise i get an ArrayIndexOutofBounds exception, for obvious reasons. And then it only seems to find the pixel, if the color is 3x the same value, eg 255,255,255, but not 255,255,254. Whats wrong there?
The format of the data buffer depends on the pixel format of the image. In your example code, each pixel is stored as 3 (4) consecutive byte values, one for each color channel, so the total size of the array is 3*pixelCount, that's the reason why for pixelCount+1 and so on... However the format returned from awt.Robot has the 4 channels packed into a single int (that's what the guy in your example code is doing with the bit shifts, packing 4 bytes into one int) so the array has as many entries as the image has pixels.
My last example way a little sloppy, I don't use Java myself anymore that often, but actually you don't need to do anything besides what I wrote. Here's a full, working example:
// finds the coordinates of the first pixel of a certain color in the specified area public static Point findPixel(Color color, Rectangle area, Robot screen) { BufferedImage image = screen.createScreenCapture(area); int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); int intColor = color.getRGB(); for (int i = 0; i < pixels.length; i++) { if (pixels[i] == intColor) { // match found, report pixel position int x = i % area.width; int y = i / area.width; return new Point(x, y); } } // no pixel with that color found, return null return null; }
|
Anyone know some good primers for learning ASP.Net? Already have programming knowledge, just need language specific stuff. Thanks in advance.
|
On July 03 2013 15:20 BoxingKangaroo wrote: Anyone know some good primers for learning ASP.Net? Already have programming knowledge, just need language specific stuff. Thanks in advance.
The Bible would be a good place to start. It can give you comfort when you despair after working with ASP.NET for a while. But no, i don't know any tutorials for it, i learned hating it by working with it.
|
On July 03 2013 15:20 BoxingKangaroo wrote: Anyone know some good primers for learning ASP.Net? Already have programming knowledge, just need language specific stuff. Thanks in advance. The "official" literature for Microsoft certifications are good in my experience, for ASP.NET and web development, it's the MTSC 70-515: http://www.amazon.com/MCTS-Self-Paced-Training-Exam-70-515/dp/0735627401
Have fun with ASP.NET, personally I love working with it.
|
Okay guys, I need to make a Collatz sequence and i've been bashing my brains out since this morning.
http://programmingbydoing.com/a/collatz-sequence.html
And my code (written in java) is: + Show Spoiler +import java.util.Scanner;
public class collatz { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); int replynr; System.out.print("Starting number: "); replynr = keyboard.nextInt(); int check = replynr % 2; while (check > 1) { if (check == 0) { int even = replynr / 2; System.out.print(even + " "); } else if (check != 0) { int odd = 3 * replynr + 1; System.out.print(odd + " "); } } } }
Please give me some suggestions. Thanks.
|
You're only making the check once. You might want to consider to move that into your while loop. Additionally you really should write down on paper what is going on, and which variables you need, because it doesn't look like much effort has gone into any resemblance of a design.
|
You should rewrite your while-loop. Just put while (true) and check if the number is even or odd and do the changes, then immediately after changing the number you check if it's 1 and then break; Remember to print the numbers after every change.
Hope I didn't help too much(?)
|
|
|
|
|
|