• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 10:38
CET 15:38
KST 23:38
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
[ASL22] Finals Preview: Sweet Victory!9[ASL22] Ro4 Preview: Mirror Mirror6[ASL22] Ro8 Preview: Within Reach5[ASL22] Ro8 Preview: In A Tizzy11[ASL22] Ro16 Preview: Holy Diver5
Community News
Evil Geniuses - shutting down14ISSL (IntoTheTV x SOOP SC2 League): Premier5Weekly Cups (October 12-18): 5.0.17 era begins1[$1250] International ShieldBattery League77Weekly Cups (October 5-11): Farewell to 5.0.167
StarCraft 2
General
How do you feel about the mass reverts in the 5.0.17 PTR? REPORT: StarCraft 2: Remastered In Development Team Liquid Map Contest #22: Results and Winners No more SC2 streamers on team liquid? Evil Geniuses - shutting down
Tourneys
ScienceCraft (October 24-25) - Live Event Sparkling Tuna Cup - Weekly Open Tournament RSL goes to London! 2026 Offline Finals Nov 21-22 ISSL (IntoTheTV x SOOP SC2 League): Premier WORTEX Challenger Series — Open SC2 LAN in Budapes
Strategy
[H] ZvP Mid-Late Game: Stalkers Collossi HT
Custom Maps
[G] How to playtest arcade maps [A] Nemrods 1/4 players [M] (2) Sweltering Sands
External Content
Mutation # 549 Endless Sparkles The PondCast: SC2 News & Results Mutation # 548 Shining Bright Mutation # 547 Temple Of Terror
Brood War
General
Recent recommended BW games [Megathread] Daily Funny Videos Hands of Pluto Oceanic Brood War - Join and chat! Bot on ladder
Tourneys
[Megathread] Daily Proleagues [$1250] International ShieldBattery League [ASL22] Grand Finals [BSL23] RO24 Group C
Strategy
Cliff Jump Revisited (1 in a 1000 strategy) 50,000-Word Terran Builds Memo 2026-10-22 Simple Questions, Simple Answers
Other Games
General Games
Nintendo Switch Thread Stormgate/Frost Giant Megathread Mobile Legends: Bang Bang Armies of Exigo - YesYes? General RTS Discussion Thread
Dota 2
Dota 2 Champions League Season 3 Begins April 25!
League of Legends
[TL LoL EUW IHs] Teemo shall perish
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Community Thread
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread Canadian Politics Mega-thread European Politico-economics QA Mega-thread
Fan Clubs
Reynor Fan Club The Flash Fan Club Serral Fan Club
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece
Sports
Football (Soccer) Thread TeamLiquid Health and Fitness Initiative For 2023 MLB/Baseball 2023
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Recent Gifted Posts
Blogs
Innovate or Game Over?
TrAiDoS
ShieldBattery on Mac
Kraekkling
[ASL22] Ro4 Day1 Ticket Giv…
bITt.mAN
Can Bots Beat Pros?? Starcr…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 7468 users

The Big Programming Thread - Page 319

Forum Index > General Forum
Post a Reply
Prev 1 317 318 319 320 321 1032 Next
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.
icystorage
Profile Blog Joined November 2008
Jollibee19350 Posts
July 02 2013 09:11 GMT
#6361
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.
LiquidDota StaffAre you ready for a Miracle-? We are! The International 2017 Champions!
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
July 02 2013 09:31 GMT
#6362
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.
netherh
Profile Blog Joined November 2011
United Kingdom333 Posts
Last Edited: 2013-07-02 09:43:03
July 02 2013 09:39 GMT
#6363
Enh, nvm.
Rannasha
Profile Blog Joined August 2010
Netherlands2398 Posts
Last Edited: 2013-07-02 09:52:45
July 02 2013 09:43 GMT
#6364
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.
Such flammable little insects!
beamerkun
Profile Joined December 2009
Poland112 Posts
July 02 2013 09:58 GMT
#6365
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 )
Evolution is complete!
Tobberoth
Profile Joined August 2010
Sweden6375 Posts
July 02 2013 10:05 GMT
#6366
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.
icystorage
Profile Blog Joined November 2008
Jollibee19350 Posts
Last Edited: 2013-07-02 11:23:38
July 02 2013 11:20 GMT
#6367
ooh okay thanks! the more you know

btw, i hope you guys took into consideration the range of n and p
LiquidDota StaffAre you ready for a Miracle-? We are! The International 2017 Champions!
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
July 02 2013 11:37 GMT
#6368
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.
icystorage
Profile Blog Joined November 2008
Jollibee19350 Posts
July 02 2013 11:41 GMT
#6369
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!
LiquidDota StaffAre you ready for a Miracle-? We are! The International 2017 Champions!
netherh
Profile Blog Joined November 2011
United Kingdom333 Posts
July 02 2013 12:07 GMT
#6370
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).
Warri
Profile Joined May 2010
Germany3208 Posts
Last Edited: 2013-07-02 17:26:00
July 02 2013 15:37 GMT
#6371
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.
bangsholt
Profile Joined June 2011
Denmark138 Posts
July 02 2013 18:49 GMT
#6372
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)
WindWolf
Profile Blog Joined July 2012
Sweden11767 Posts
July 02 2013 18:56 GMT
#6373
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
EZ4ENCE
ferdkuh
Profile Joined January 2013
10 Posts
Last Edited: 2013-07-03 01:40:49
July 03 2013 01:39 GMT
#6374
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;
}
BoxingKangaroo
Profile Blog Joined December 2011
Japan955 Posts
July 03 2013 06:20 GMT
#6375
Anyone know some good primers for learning ASP.Net? Already have programming knowledge, just need language specific stuff. Thanks in advance.
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
July 03 2013 10:13 GMT
#6376
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.
Tobberoth
Profile Joined August 2010
Sweden6375 Posts
July 03 2013 10:22 GMT
#6377
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.
HyunA
Profile Joined July 2010
Romania362 Posts
Last Edited: 2013-07-03 15:36:30
July 03 2013 15:28 GMT
#6378
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.
bangsholt
Profile Joined June 2011
Denmark138 Posts
Last Edited: 2013-07-03 15:43:11
July 03 2013 15:41 GMT
#6379
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.
Intgrl
Profile Joined June 2012
27 Posts
July 03 2013 15:45 GMT
#6380
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(?)
among the mindless
Prev 1 317 318 319 320 321 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 9h 22m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Rex 121
LamboSC2 6
StarCraft: Brood War
Britney 46819
Calm 7510
Bisu 2223
Mini 1055
Horang2 805
Jaedong 570
Soma 552
Larva 496
EffOrt 433
Stork 412
[ Show more ]
Light 353
BeSt 340
firebathero 202
Barracks 129
hero 107
Sea.KH 85
Zeus 79
ggaemo 76
Snow 62
Mind 59
Sharp 56
Mong 42
Killer 41
Bishop 34
Yoon 33
ToSsGirL 26
HiyA 26
Terrorterran 24
Movie 23
910 23
Rock 22
Sexy 18
zelot 17
scan(afreeca) 14
Shine 12
ajuk12(nOOB) 10
Sacsri 10
yabsab 10
Dota 2
Gorgc8032
Cr1tdota2496
qojqva1164
syndereN273
XcaliburYe224
Counter-Strike
olofmeister806
Heroes of the Storm
Trikslyr75
Other Games
B2W.Neo1333
hiko1025
Beastyqt530
crisheroes434
Lowko333
Pyrionflax232
DeMusliM174
Hui .167
Liquid`VortiX123
RotterdaM68
QueenE25
fpsfer 2
Organizations
Counter-Strike
PGL20487
StarCraft 2
IntoTheiNu 990
WardiTV606
CranKy Ducklings72
[ Show 12 non-featured ]
StarCraft 2
• CranKy Ducklings SOOP7
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• Migwel
StarCraft: Brood War
• FirePhoenix2
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• WagamamaTV559
League of Legends
• TFBlade406
Upcoming Events
PiGosaur Cup
9h 22m
The PondCast
19h 22m
Kung Fu Cup
20h 22m
IntoTheTV X SOOP
1d 20h
SHIN vs GuMiho
Clem vs Classic
OSC
2 days
WardiTV Invitational
2 days
GSL
2 days
Epic.LAN
2 days
Replay Cast
3 days
Sparkling Tuna Cup
3 days
[ Show More ]
Epic.LAN
3 days
Yamato Cup
5 days
Map Test Tournament
5 days
WardiTV Weekly
5 days
Replay Cast
6 days
Rogue vs Classic
Patches Events
6 days
Liquipedia Results

Completed

Acropolis #5 - GSC
Blizzard Classic Cup 2026
HCC Season 3

Ongoing

Acropolis #5
KCM Race Survival 2026 Season 4
International ShieldBattery League
BSL Season 23
PGL Masters Bucharest 2026
Stake Ranked Episode 5
1win Private Club #2
Thunderpick World Champ. '26
ESL Pro League Season 24
Stake Ranked Episode 4
1win Private Club #1
Logitech G Play Connect 2026
SL StarSeries Fall 2026
FISSURE Playground #3
BLAST Open Fall 2026
Esports World Cup 2026

Upcoming

IPSL Winter 2026-27
SC4ALL II: Brood War
BSL 23: Non-Korean Championship
HSC XXX
Stellar Fest 2: Lunar Cup
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
RSL Offline Finals
eXTREMESLAND 2026
PGL Major Singapore 2026
Stake Ranked Episode 6
BLAST Rivals Fall 2026
IEM Beijing 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Disclosure: This page contains affiliate marketing links that support TLnet.

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.