• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 13:29
CEST 19:29
KST 02: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
Team Liquid Map Contest #22 - The Finalists5[ASL21] Ro16 Preview Pt1: Fresh Flow9[ASL21] Ro24 Preview Pt2: News Flash10[ASL21] Ro24 Preview Pt1: New Chaos0Team Liquid Map Contest #22 - Presented by Monster Energy21
Community News
2026 GSL Tour plans announced0Weekly Cups (April 6-12): herO doubles, "Villains" prevail0MaNa leaves Team Liquid18$5,000 WardiTV TLMC tournament - Presented by Monster Energy5GSL CK: More events planned pending crowdfunding7
StarCraft 2
General
2026 GSL Tour plans announced Team Liquid Map Contest #22 - The Finalists Weekly Cups (April 6-12): herO doubles, "Villains" prevail MaNa leaves Team Liquid Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool
Tourneys
2026 GSL Season 1 Qualifiers Master Swan Open (Global Bronze-Master 2) SEL Doubles (SC Evo Bimonthly) Sparkling Tuna Cup - Weekly Open Tournament $5,000 WardiTV TLMC tournament - Presented by Monster Energy
Strategy
Custom Maps
[D]RTS in all its shapes and glory <3 [A] Nemrods 1/4 players [M] (2) Frigid Storage
External Content
Mutation # 521 Memorable Boss The PondCast: SC2 News & Results Mutation # 520 Moving Fees Mutation # 519 Inner Power
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ A cwal.gg Extension - Easily keep track of anyone [ASL21] Ro16 Preview Pt1: Fresh Flow BW General Discussion ASL21 Strategy, Pimpest Plays Discussions
Tourneys
[ASL21] Ro24 Group F [ASL21] Ro16 Group B [ASL21] Ro16 Group A [Megathread] Daily Proleagues
Strategy
What's the deal with APM & what's its true value Any training maps people recommend? Fighting Spirit mining rates Muta micro map competition
Other Games
General Games
Nintendo Switch Thread Battle Aces/David Kim RTS Megathread Stormgate/Frost Giant Megathread General RTS Discussion Thread Starcraft Tabletop Miniature Game
Dota 2
The Story of Wings Gaming Official 'what is Dota anymore' discussion
League of Legends
G2 just beat GenG in First stand
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
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas TL Mafia Community Thread Five o'clock TL Mafia
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread Russo-Ukrainian War Thread European Politico-economics QA Mega-thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
[Req][Books] Good Fantasy/SciFi books [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion Cricket [SPORT] Tokyo Olympics 2021 Thread
World Cup 2022
Tech Support
[G] How to Block Livestream Ads
TL Community
The Automated Ban List
Blogs
Reappraising The Situation T…
TrAiDoS
lurker extra damage testi…
StaticNine
Broowar part 2
qwaykee
Funny Nicknames
LUCKY_NOOB
Iranian anarchists: organize…
XenOsky
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1997 users

Math/Computer Science Problem

Blogs > Slithe
Post a Reply
Slithe
Profile Blog Joined February 2007
United States985 Posts
Last Edited: 2008-01-22 23:20:36
January 22 2008 23:17 GMT
#1
You have a list of unknown size N, that you are only allowed to traverse once. The goal is to devise a method to create a set of size K that is random and uniformly distributed among all the possible subsets of N that are size K. What is the method?

Remember:
1) You do not know N. Your answer should not rely on N, i.e. your memory size should not be based on N in any way.
2) You can only traverse the list once.
3) K is less than or equal to N, so there is always at least one possible set.

*****
Asta
Profile Joined October 2002
Germany3491 Posts
January 22 2008 23:39 GMT
#2
I'm too tired to think it through but will the 'picks' be evenly distributed if you choose the first K elements and then (if N > K) eliminate a random (evenly distributed) element of the K+1 (the old picks and the next one on the list)?
I think not... :D Because the first ones will be much likelier to be discarded than the last ones. Which brings us back to the initial problem that you don't know to what degree you have to favor the first ones because it depends on N. Hum...
Cascade
Profile Blog Joined March 2006
Australia5405 Posts
January 23 2008 00:31 GMT
#3
Yeah, that's how you must do it. Up to K you obvioulsy have to pick all elements to your set. When you run through the n:th element (n>K), you use it to replace a random element in your previous set of K elements with a probability K/n. Leave the set unchanged with prob (n-K)/n. Use induction to prove that it works.
zdd
Profile Blog Joined October 2004
1463 Posts
Last Edited: 2008-01-23 02:49:19
January 23 2008 00:31 GMT
#4
I think it would be something like this:
let's say n is an array of ascending integers, k is an int, set_k is an empty array

the set n would be distributed something like this: [1 - k][k+1 - 2k][2k+1 - 3k]...
i=0;
for each $element in n {
if(i<k){
if(random(1,k)=k){
$set_k[i]=$element;
i++;
}
}
}

I dunno, statistically each element in n would have a 1/k chance to become an element in set_k, so I guess in every k elements in set n there should only be around 1 element that goes into set k.

edit: oh nvm, maybe you could just copy set "n" to set "set_k", then remove random elements from set_k until length(set_k) is equal to K

int K = <some value>;
int i=0;
int[] set_k;
for each $element in n {
set_k[i]=$element;
i++;
}
while(length(set_k) != K) {
delete(set_k[random(0, length(set_k))]);
}
output set_k

edit2: cascade's idea is different, because he uses k+1 memory instead of n
it would be implemented something like:
int K = <some value>;
int i=0;
int[] set_k;
function randomkn(k, n){ //run random(1,n) k times
$var=false;
while (k>0){
k--;
if (random(1, n) == n){$var=true;}
}
return $var;
}
for each $element in n {

if (i<K+1){
set_k[i]=$element;
i++;
}

else if (randomkn(K, length(n))) {
set_k[random(0,K+1)]=$element;
}

}
output set_k
All you need in life is a strong will to succeed and unrelenting determination. If you meet these prerequisites, you can become anything you want with absolutely no luck, fortune or natural ability.
BottleAbuser
Profile Blog Joined December 2007
Korea (South)1888 Posts
Last Edited: 2008-01-23 01:37:02
January 23 2008 01:30 GMT
#5
zdd, your second algorithm isn't O(1) or O(K) memory, it's O(n) memory, which breaks the problem constraints.

I'm not sure if I follow your first one.

I think the actual solution has something to do with finding the probability that the nth element should be included in the set, given k.

Obviously (at least to me, I could be wrong), the first element should be selected with (N/K) probability. Then, k := K - 1 if the first element was selected, and n := N - 1 , and repeat for the rest of the elements. This is guaranteed to return K elements.

There might seem to be a problem with the later elements (especially the last element) having a higher than (N/K) probability to be selected, but I think this is not the case. For the last L elements to always be selected, (n/k == 1) must hold. However, the probability of this happening is quite small if N is large (unless K is equally large).

I'm too lazy to calculate it myself (OK, I'm too stupid to do it), but I think if you go through with it, you'll find that the probability that the last L elements are arrived at with (k==n) is the same as 1 minus the probability that they are arrived at with (k==0).

Oh, shit, you don't know N. My idea is bunk.
Compilers are like boyfriends, you miss a period and they go crazy on you.
BottleAbuser
Profile Blog Joined December 2007
Korea (South)1888 Posts
January 23 2008 01:35 GMT
#6
Yeah, I think that Cascade's idea will work. Your memory space is O(K), not O(N), so it's fine.
Compilers are like boyfriends, you miss a period and they go crazy on you.
Slithe
Profile Blog Joined February 2007
United States985 Posts
January 23 2008 02:41 GMT
#7
Yes Cascade has the right answer. Good job! I'll be posting up a significantly more difficult problem now, which I actually don't quite know the answer to, but I have a basic idea.

Regarding this problem:
Basically, since you don't know how many elements are in the list, you have to make sure at each iteration through the list, all elements up to that iteration have an equal probability, k/i, of being in the set, where "i" is the current index. If you work around that idea, then the answer comes more easily, or at least for me it did.

For Asta's answer: The key there was that you don't always discard. It seems that you already realized the problem, so you probably would have realized the fix eventually.

For zdd's answers:
The first one is incorrect because, all elements will have probably 1/k of being in the set until the set is full, and then the next elements have probability 0. In the first place, 1/k is incorrect, and the correct probability to aim for is k/i.
The second one, BottleAbuser pointed out the problem, which is that you require O(n) memory, which is not allowed.
Please log in or register to reply.
Live Events Refresh
Next event in 6h 31m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
mouzHeroMarine 328
elazer 253
ProTech127
BRAT_OK 34
UpATreeSC 32
MindelVK 12
StarCraft: Brood War
Calm 4510
Bisu 3316
Jaedong 1176
Mini 554
Soma 411
Larva 315
BeSt 307
ZerO 215
Light 195
firebathero 182
[ Show more ]
Dewaltoss 151
Rush 127
Soulkey 103
Aegong 68
Hyun 63
Shinee 55
soO 35
sorry 32
Rock 19
Sexy 15
Bale 11
Terrorterran 8
ivOry 6
Dota 2
Gorgc8406
qojqva2087
Counter-Strike
fl0m7029
olofmeister6716
pashabiceps1116
byalli694
Super Smash Bros
Mew2King95
Other Games
Grubby2541
FrodaN1473
Beastyqt761
ceh9444
Hui .167
ArmadaUGS128
QueenE95
C9.Mang050
ZerO(Twitch)11
B2W.Neo0
Organizations
Other Games
BasetradeTV311
Counter-Strike
PGL140
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 20 non-featured ]
StarCraft 2
• StrangeGG 61
• Adnapsc2 18
• Kozan
• sooper7s
• Migwel
• AfreecaTV YouTube
• LaughNgamezSOOP
• intothetv
• IndyKCrew
StarCraft: Brood War
• HerbMon 10
• FirePhoenix2
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• lizZardDota262
League of Legends
• Nemesis2896
• TFBlade1493
• Jankos482
Other Games
• imaqtpie592
• Shiphtur208
Upcoming Events
PiGosaur Cup
6h 31m
CranKy Ducklings
15h 31m
Kung Fu Cup
18h 31m
Replay Cast
1d 6h
The PondCast
1d 16h
WardiTV Map Contest Tou…
1d 17h
Replay Cast
2 days
Escore
2 days
WardiTV Map Contest Tou…
2 days
OSC
2 days
[ Show More ]
Korean StarCraft League
3 days
CranKy Ducklings
3 days
WardiTV Map Contest Tou…
3 days
IPSL
3 days
WolFix vs nOmaD
dxtr13 vs Razz
BSL
4 days
Sparkling Tuna Cup
4 days
WardiTV Map Contest Tou…
4 days
Ladder Legends
4 days
BSL
5 days
IPSL
5 days
JDConan vs TBD
Aegong vs rasowy
Replay Cast
5 days
Replay Cast
5 days
Wardi Open
5 days
Afreeca Starleague
5 days
Bisu vs Ample
Jaedong vs Flash
Monday Night Weeklies
5 days
RSL Revival
6 days
Afreeca Starleague
6 days
Barracks vs Leta
Royal vs Light
WardiTV Map Contest Tou…
6 days
Liquipedia Results

Completed

Proleague 2026-04-13
RSL Revival: Season 4
NationLESS Cup

Ongoing

BSL Season 22
ASL Season 21
CSL 2026 SPRING (S20)
IPSL Spring 2026
StarCraft2 Community Team League 2026 Spring
Nations Cup 2026
IEM Rio 2026
PGL Bucharest 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026
IEM Kraków 2026

Upcoming

Escore Tournament S2: W3
Acropolis #4
BSL 22 Non-Korean Championship
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
RSL Revival: Season 5
WardiTV TLMC #16
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
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...

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.