• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 19:29
CEST 01:29
KST 08:29
  • 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] Ro16 Preview: Rough Waters8[ASL22] Ro24 Preview: Siren's Call8[ASL22] Ro24 Preview: Summer's End9Serral wins HomeStory Cup 2915Serral wins Maestros of the Game 244
Community News
Weekly Cups (Aug 30-Sep 7): herO thrives amid growing schism4Official StarCraft website teases new content ahead of BlizzCon?117Stellar Fest TWO the Moon (Dec 16-20)9Weekly Cups (August 24-30): Patches' balance mod takes over3New 3v3 BGH Ladder (and more) on ShieldBattery!46
StarCraft 2
General
Weekly Cups (Aug 30-Sep 7): herO thrives amid growing schism Nexon wins bid to develop StarCraft IP content, distribute Overwatch mobile game Balance hotfix patch 5.0.16b (July 16) SC4ALL: II Winner Will Earn a Spot at HSC 30! Weekly Cups (August 24-30): Patches' balance mod takes over
Tourneys
Sea Duckling Open (Global, Bronze-Diamond) 2026 GSTL Announcement Sparkling Tuna Cup - Weekly Open Tournament Stellar Fest TWO the Moon (Dec 16-20) IntoTheTV X SOOP SC2 League : Weekly & Monthly
Strategy
[G] Having the right mentality to improve
Custom Maps
Nexus Wars 2021 GUIDE [M] (2) Industrial Park
External Content
Mutation # 542 The Ascended Mutation # 541 Binary Choice The PondCast: SC2 News & Results Mutation # 540 Dodge This
Brood War
General
Official StarCraft website teases new content ahead of BlizzCon? ASL22 General Discussion Pros React To: Calm's Big Brain Play Vs Mini New 3v3 BGH Ladder (and more) on ShieldBattery! BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[Megathread] Daily Proleagues [ASL22] Ro16 Group B 2v2v2v2 Tournament [ASL22] Ro16 Group A
Strategy
Replay Review Process - What do you do? Simple Questions, Simple Answers Odyssey Mineral Stack Saturation Game Theory for Starcraft
Other Games
General Games
Nintendo Switch Thread EVE Corporation Diablo IV [Maplestory Hardcore] Let's Play~!! General RTS Discussion Thread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
[TL LoL EUW IHs] Teemo shall perish TSM pausing esports and CLG Dead
Heroes of the Storm
Heroes of the Storm 2.0
Hearthstone
Deck construction bug
TL Mafia
TL Mafia Power Rank TL Mafia Community Thread NeO.D_StephenKing vs This Guy From 1 Million Dance
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine UK Politics Mega-thread Artificial Intelligence Thread
Fan Clubs
MarineLorD Fan Club The Creator Fan Club The ShoWTimE Fan Club
Media & Entertainment
Movie Discussion! Anime Discussion Thread
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
The Automated Ban List Northern Ireland Global Starcraft
Blogs
F* the Children and Get Off …
TrAiDoS
Dreaming of BW patches (mod…
c3rberUs
Regacy Esports: The Bh…
regacyesports
LOCKPICKING NOOB
LUCKY_NOOB
Customize Sidebar...

Website Feedback

Closed Threads



Active: 8582 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 31m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
ProTech135
SpeCial 125
CosmosSc2 46
JuggernautJason33
StarCraft: Brood War
Britney 8952
NaDa 22
Yoon 9
Dota 2
monkeys_forever379
Counter-Strike
Coldzera 1361
minikerr41
Super Smash Bros
PPMD53
Other Games
summit1g9457
shahzam590
Day[9].tv380
C9.Mang0272
RotterdaM106
ViBE53
[ Show 12 non-featured ]
StarCraft 2
• RyuSc2 29
• davetesta15
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• Migwel
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• masondota22102
Other Games
• Day9tv380
Upcoming Events
PiGosaur Cup
31m
Kung Fu Cup
11h 31m
Replay Cast
1d
The PondCast
1d 10h
KCM Race Survival
1d 10h
Escore
2 days
IntoTheTV X SOOP
2 days
CranKy Ducklings
3 days
Sparkling Tuna Cup
4 days
Shopify Rebellion Sundays
4 days
Spirit vs Mixu
Clem vs TBD
[ Show More ]
RSL Revival
4 days
BlizzCon
4 days
Replay Cast
5 days
Afreeca Starleague
5 days
WardiTV Weekly
5 days
Afreeca Starleague
6 days
GSL
6 days
Liquipedia Results

Completed

Proleague 2026-09-02
PiG Sty Festival 8.0
Big Dog Cup 2026 Div 1

Ongoing

KCM Race Survival 2026 Season 3
K-JUNGMAN
ASL Season 22
Super Anchor Qualifying S3
CSL 2026 AUTUMN (S22)
Acropolis #5
Acropolis #5 - TRS
RSL Revival: Season 6
Calamity Invitational
FISSURE Playground #3
BLAST Open Fall 2026
Esports World Cup 2026
BLAST Bounty Summer 2026
BLAST Bounty Summer Qual
Stake Ranked Episode 3
XSE Pro League 2026

Upcoming

Escore Tournament S3: King of Kings
Acropolis #5 - GSA
Blizzard Classic Cup 2026
Acropolis #5 - GSB
Acropolis #5 - GSC
SC4ALL II: Brood War
HSC XXX
Stellar Fest 2: Lunar Cup
SC4ALL II: StarCraft II
Kung Fu Cup 2026 Grand Finals
RSL Offline Finals
Blizzard Classic Cup 2026
Stake Ranked Episode 6
BLAST Rivals Fall 2026
IEM Beijing 2026
Stake Ranked Episode 5
PGL Masters Bucharest 2026
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
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.