• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 02:06
CET 08:06
KST 16:06
  • 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
RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12Intel X Team Liquid Seoul event: Showmatches and Meet the Pros10
Community News
Weekly Cups (Nov 24-30): MaxPax, Clem, herO win2BGE Stara Zagora 2026 announced15[BSL21] Ro.16 Group Stage (C->B->A->D)4Weekly Cups (Nov 17-23): Solar, MaxPax, Clem win3RSL Season 3: RO16 results & RO8 bracket13
StarCraft 2
General
Chinese SC2 server to reopen; live all-star event in Hangzhou Maestros of the Game: Live Finals Preview (RO4) BGE Stara Zagora 2026 announced Weekly Cups (Nov 24-30): MaxPax, Clem, herO win SC2 Proleague Discontinued; SKT, KT, SGK, CJ disband
Tourneys
StarCraft Evolution League (SC Evo Biweekly) RSL Offline Finals Info - Dec 13 and 14! RSL Offline FInals Sea Duckling Open (Global, Bronze-Diamond) $5,000+ WardiTV 2025 Championship
Strategy
Custom Maps
Map Editor closed ?
External Content
Mutation # 502 Negative Reinforcement Mutation # 501 Price of Progress Mutation # 500 Fright night Mutation # 499 Chilling Adaptation
Brood War
General
BW General Discussion Which season is the best in ASL? Data analysis on 70 million replays BGH Auto Balance -> http://bghmmr.eu/ [ASL20] Ask the mapmakers — Drop your questions
Tourneys
[BSL21] RO16 Group D - Sunday 21:00 CET [BSL21] RO16 Group A - Saturday 21:00 CET [Megathread] Daily Proleagues [BSL21] RO16 Group B - Sunday 21:00 CET
Strategy
Current Meta Game Theory for Starcraft How to stay on top of macro? PvZ map balance
Other Games
General Games
Stormgate/Frost Giant Megathread ZeroSpace Megathread Nintendo Switch Thread The Perfect Game Path of Exile
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Mafia Game Mode Feedback/Ideas TL Mafia Community Thread
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Russo-Ukrainian War Thread The Big Programming Thread Artificial Intelligence Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! Anime Discussion Thread
Sports
2024 - 2026 Football Thread Formula 1 Discussion NBA General Discussion
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
Where to ask questions and add stream? The Automated Ban List
Blogs
James Bond movies ranking - pa…
Topin
Esports Earnings: Bigger Pri…
TrAiDoS
Thanks for the RSL
Hildegard
Saturation point
Uldridge
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1385 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
Replay Cast
00:00
WardiTV Mondays #62
LiquipediaDiscussion
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
SortOf 112
-ZergGirl 29
StarCraft: Brood War
Stork 937
Tasteless 229
Sacsri 102
Shine 54
Bale 49
ZergMaN 20
Dota 2
febbydoto373
NeuroSwarm159
League of Legends
JimRising 608
Other Games
summit1g11228
WinterStarcraft562
C9.Mang0280
crisheroes109
RuFF_SC297
Mew2King48
Organizations
Other Games
gamesdonequick689
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 13 non-featured ]
StarCraft 2
• Berry_CruncH156
• practicex 40
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
League of Legends
• Lourlo1254
Upcoming Events
The PondCast
2h 55m
OSC
8h 55m
Demi vs Mixu
Nicoract vs TBD
Babymarine vs MindelVK
ForJumy vs TBD
Shameless vs Percival
Replay Cast
16h 55m
Korean StarCraft League
1d 19h
CranKy Ducklings
2 days
WardiTV 2025
2 days
SC Evo League
2 days
BSL 21
2 days
Sziky vs OyAji
Gypsy vs eOnzErG
OSC
2 days
Solar vs Creator
ByuN vs Gerald
Percival vs Babymarine
Moja vs Krystianer
EnDerr vs ForJumy
sebesdes vs Nicoract
Sparkling Tuna Cup
3 days
[ Show More ]
WardiTV 2025
3 days
OSC
3 days
BSL 21
3 days
Bonyth vs StRyKeR
Tarson vs Dandy
Replay Cast
4 days
Wardi Open
4 days
StarCraft2.fi
4 days
Monday Night Weeklies
4 days
Replay Cast
4 days
WardiTV 2025
5 days
StarCraft2.fi
5 days
PiGosaur Monday
5 days
StarCraft2.fi
6 days
Tenacious Turtle Tussle
6 days
Liquipedia Results

Completed

Proleague 2025-11-30
RSL Revival: Season 3
Light HT

Ongoing

C-Race Season 1
IPSL Winter 2025-26
KCM Race Survival 2025 Season 4
YSL S2
BSL Season 21
CSCL: Masked Kings S3
Slon Tour Season 2
Acropolis #4 - TS3
META Madness #9
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2

Upcoming

BSL 21 Non-Korean Championship
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
RSL Offline Finals
WardiTV 2025
Kuram Kup
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
BLAST Bounty Winter Qual
eXTREMESLAND 2025
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 © 2025 TLnet. All Rights Reserved.