To string or not to string
$begingroup$
Given an input string, output at random the unique combinations with repetition of the characters in the input string, from length 1 up to the length of the input string, with an equal chance of each one occurring.
Example: given the input abcd
(or any combination thereof of the four characters a
,b
,c
,d
) , there is an equal chance of outputting:
a b c d aa ab ac ad ba bb bc bd ca cb cc cd da db dc dd aaa aab aac aad aba abb abc abd aca acb acc acd ada adb adc add baa bab bac bad bba bbb bbc bbd bca bcb bcc bcd bda bdb bdc bdd caa cab cac cad cba cbb cbc cbd cca ccb ccc ccd cda cdb cdc cdd daa dab dac dad dba dbb dbc dbd dca dcb dcc dcd dda ddb ddc ddd aaaa aaab aaac aaad aaba aabb aabc aabd aaca aacb aacc aacd aada aadb aadc aadd abaa abab abac abad abba abbb abbc abbd abca abcb abcc abcd abda abdb abdc abdd acaa acab acac acad acba acbb acbc acbd acca accb accc accd acda acdb acdc acdd adaa adab adac adad adba adbb adbc adbd adca adcb adcc adcd adda addb addc addd baaa baab baac baad baba babb babc babd baca bacb bacc bacd bada badb badc badd bbaa bbab bbac bbad bbba bbbb bbbc bbbd bbca bbcb bbcc bbcd bbda bbdb bbdc bbdd bcaa bcab bcac bcad bcba bcbb bcbc bcbd bcca bccb bccc bccd bcda bcdb bcdc bcdd bdaa bdab bdac bdad bdba bdbb bdbc bdbd bdca bdcb bdcc bdcd bdda bddb bddc bddd caaa caab caac caad caba cabb cabc cabd caca cacb cacc cacd cada cadb cadc cadd cbaa cbab cbac cbad cbba cbbb cbbc cbbd cbca cbcb cbcc cbcd cbda cbdb cbdc cbdd ccaa ccab ccac ccad ccba ccbb ccbc ccbd ccca cccb cccc cccd ccda ccdb ccdc ccdd cdaa cdab cdac cdad cdba cdbb cdbc cdbd cdca cdcb cdcc cdcd cdda cddb cddc cddd daaa daab daac daad daba dabb dabc dabd daca dacb dacc dacd dada dadb dadc dadd dbaa dbab dbac dbad dbba dbbb dbbc dbbd dbca dbcb dbcc dbcd dbda dbdb dbdc dbdd dcaa dcab dcac dcad dcba dcbb dcbc dcbd dcca dccb dccc dccd dcda dcdb dcdc dcdd ddaa ddab ddac ddad ddba ddbb ddbc ddbd ddca ddcb ddcc ddcd ddda dddb dddc dddd
Example: given the input efgh
(or any combination thereof of the four characters e
,f
,g
,h
), there is an equal chance of outputting:
e f g h ee ef eg eh fe ff fg fh ge gf gg gh he hf hg hh eee eef eeg eeh efe eff efg efh ege egf egg egh ehe ehf ehg ehh fee fef feg feh ffe fff ffg ffh fge fgf fgg fgh fhe fhf fhg fhh gee gef geg geh gfe gff gfg gfh gge ggf ggg ggh ghe ghf ghg ghh hee hef heg heh hfe hff hfg hfh hge hgf hgg hgh hhe hhf hhg hhh eeee eeef eeeg eeeh eefe eeff eefg eefh eege eegf eegg eegh eehe eehf eehg eehh efee efef efeg efeh effe efff effg effh efge efgf efgg efgh efhe efhf efhg efhh egee egef egeg egeh egfe egff egfg egfh egge eggf eggg eggh eghe eghf eghg eghh ehee ehef eheg eheh ehfe ehff ehfg ehfh ehge ehgf ehgg ehgh ehhe ehhf ehhg ehhh feee feef feeg feeh fefe feff fefg fefh fege fegf fegg fegh fehe fehf fehg fehh ffee ffef ffeg ffeh fffe ffff fffg fffh ffge ffgf ffgg ffgh ffhe ffhf ffhg ffhh fgee fgef fgeg fgeh fgfe fgff fgfg fgfh fgge fggf fggg fggh fghe fghf fghg fghh fhee fhef fheg fheh fhfe fhff fhfg fhfh fhge fhgf fhgg fhgh fhhe fhhf fhhg fhhh geee geef geeg geeh gefe geff gefg gefh gege gegf gegg gegh gehe gehf gehg gehh gfee gfef gfeg gfeh gffe gfff gffg gffh gfge gfgf gfgg gfgh gfhe gfhf gfhg gfhh ggee ggef ggeg ggeh ggfe ggff ggfg ggfh ggge gggf gggg gggh gghe gghf gghg gghh ghee ghef gheg gheh ghfe ghff ghfg ghfh ghge ghgf ghgg ghgh ghhe ghhf ghhg ghhh heee heef heeg heeh hefe heff hefg hefh hege hegf hegg hegh hehe hehf hehg hehh hfee hfef hfeg hfeh hffe hfff hffg hffh hfge hfgf hfgg hfgh hfhe hfhf hfhg hfhh hgee hgef hgeg hgeh hgfe hgff hgfg hgfh hgge hggf hggg hggh hghe hghf hghg hghh hhee hhef hheg hheh hhfe hhff hhfg hhfh hhge hhgf hhgg hhgh hhhe hhhf hhhg hhhh
Example: given the input ijkl
(or any combination thereof of the four characters i
,j
,k
,l
), there is an equal chance of outputting:
i j k l ii ij ik il ji jj jk jl ki kj kk kl li lj lk ll iii iij iik iil iji ijj ijk ijl iki ikj ikk ikl ili ilj ilk ill jii jij jik jil jji jjj jjk jjl jki jkj jkk jkl jli jlj jlk jll kii kij kik kil kji kjj kjk kjl kki kkj kkk kkl kli klj klk kll lii lij lik lil lji ljj ljk ljl lki lkj lkk lkl lli llj llk lll iiii iiij iiik iiil iiji iijj iijk iijl iiki iikj iikk iikl iili iilj iilk iill ijii ijij ijik ijil ijji ijjj ijjk ijjl ijki ijkj ijkk ijkl ijli ijlj ijlk ijll ikii ikij ikik ikil ikji ikjj ikjk ikjl ikki ikkj ikkk ikkl ikli iklj iklk ikll ilii ilij ilik ilil ilji iljj iljk iljl ilki ilkj ilkk ilkl illi illj illk illl jiii jiij jiik jiil jiji jijj jijk jijl jiki jikj jikk jikl jili jilj jilk jill jjii jjij jjik jjil jjji jjjj jjjk jjjl jjki jjkj jjkk jjkl jjli jjlj jjlk jjll jkii jkij jkik jkil jkji jkjj jkjk jkjl jkki jkkj jkkk jkkl jkli jklj jklk jkll jlii jlij jlik jlil jlji jljj jljk jljl jlki jlkj jlkk jlkl jlli jllj jllk jlll kiii kiij kiik kiil kiji kijj kijk kijl kiki kikj kikk kikl kili kilj kilk kill kjii kjij kjik kjil kjji kjjj kjjk kjjl kjki kjkj kjkk kjkl kjli kjlj kjlk kjll kkii kkij kkik kkil kkji kkjj kkjk kkjl kkki kkkj kkkk kkkl kkli kklj kklk kkll klii klij klik klil klji kljj kljk kljl klki klkj klkk klkl klli kllj kllk klll liii liij liik liil liji lijj lijk lijl liki likj likk likl lili lilj lilk lill ljii ljij ljik ljil ljji ljjj ljjk ljjl ljki ljkj ljkk ljkl ljli ljlj ljlk ljll lkii lkij lkik lkil lkji lkjj lkjk lkjl lkki lkkj lkkk lkkl lkli lklj lklk lkll llii llij llik llil llji lljj lljk lljl llki llkj llkk llkl llli lllj lllk llll
This by definition also means that every indiviual character within the string too has equal probability. All characters in the input are distinct.
code-golf string random
$endgroup$
|
show 14 more comments
$begingroup$
Given an input string, output at random the unique combinations with repetition of the characters in the input string, from length 1 up to the length of the input string, with an equal chance of each one occurring.
Example: given the input abcd
(or any combination thereof of the four characters a
,b
,c
,d
) , there is an equal chance of outputting:
a b c d aa ab ac ad ba bb bc bd ca cb cc cd da db dc dd aaa aab aac aad aba abb abc abd aca acb acc acd ada adb adc add baa bab bac bad bba bbb bbc bbd bca bcb bcc bcd bda bdb bdc bdd caa cab cac cad cba cbb cbc cbd cca ccb ccc ccd cda cdb cdc cdd daa dab dac dad dba dbb dbc dbd dca dcb dcc dcd dda ddb ddc ddd aaaa aaab aaac aaad aaba aabb aabc aabd aaca aacb aacc aacd aada aadb aadc aadd abaa abab abac abad abba abbb abbc abbd abca abcb abcc abcd abda abdb abdc abdd acaa acab acac acad acba acbb acbc acbd acca accb accc accd acda acdb acdc acdd adaa adab adac adad adba adbb adbc adbd adca adcb adcc adcd adda addb addc addd baaa baab baac baad baba babb babc babd baca bacb bacc bacd bada badb badc badd bbaa bbab bbac bbad bbba bbbb bbbc bbbd bbca bbcb bbcc bbcd bbda bbdb bbdc bbdd bcaa bcab bcac bcad bcba bcbb bcbc bcbd bcca bccb bccc bccd bcda bcdb bcdc bcdd bdaa bdab bdac bdad bdba bdbb bdbc bdbd bdca bdcb bdcc bdcd bdda bddb bddc bddd caaa caab caac caad caba cabb cabc cabd caca cacb cacc cacd cada cadb cadc cadd cbaa cbab cbac cbad cbba cbbb cbbc cbbd cbca cbcb cbcc cbcd cbda cbdb cbdc cbdd ccaa ccab ccac ccad ccba ccbb ccbc ccbd ccca cccb cccc cccd ccda ccdb ccdc ccdd cdaa cdab cdac cdad cdba cdbb cdbc cdbd cdca cdcb cdcc cdcd cdda cddb cddc cddd daaa daab daac daad daba dabb dabc dabd daca dacb dacc dacd dada dadb dadc dadd dbaa dbab dbac dbad dbba dbbb dbbc dbbd dbca dbcb dbcc dbcd dbda dbdb dbdc dbdd dcaa dcab dcac dcad dcba dcbb dcbc dcbd dcca dccb dccc dccd dcda dcdb dcdc dcdd ddaa ddab ddac ddad ddba ddbb ddbc ddbd ddca ddcb ddcc ddcd ddda dddb dddc dddd
Example: given the input efgh
(or any combination thereof of the four characters e
,f
,g
,h
), there is an equal chance of outputting:
e f g h ee ef eg eh fe ff fg fh ge gf gg gh he hf hg hh eee eef eeg eeh efe eff efg efh ege egf egg egh ehe ehf ehg ehh fee fef feg feh ffe fff ffg ffh fge fgf fgg fgh fhe fhf fhg fhh gee gef geg geh gfe gff gfg gfh gge ggf ggg ggh ghe ghf ghg ghh hee hef heg heh hfe hff hfg hfh hge hgf hgg hgh hhe hhf hhg hhh eeee eeef eeeg eeeh eefe eeff eefg eefh eege eegf eegg eegh eehe eehf eehg eehh efee efef efeg efeh effe efff effg effh efge efgf efgg efgh efhe efhf efhg efhh egee egef egeg egeh egfe egff egfg egfh egge eggf eggg eggh eghe eghf eghg eghh ehee ehef eheg eheh ehfe ehff ehfg ehfh ehge ehgf ehgg ehgh ehhe ehhf ehhg ehhh feee feef feeg feeh fefe feff fefg fefh fege fegf fegg fegh fehe fehf fehg fehh ffee ffef ffeg ffeh fffe ffff fffg fffh ffge ffgf ffgg ffgh ffhe ffhf ffhg ffhh fgee fgef fgeg fgeh fgfe fgff fgfg fgfh fgge fggf fggg fggh fghe fghf fghg fghh fhee fhef fheg fheh fhfe fhff fhfg fhfh fhge fhgf fhgg fhgh fhhe fhhf fhhg fhhh geee geef geeg geeh gefe geff gefg gefh gege gegf gegg gegh gehe gehf gehg gehh gfee gfef gfeg gfeh gffe gfff gffg gffh gfge gfgf gfgg gfgh gfhe gfhf gfhg gfhh ggee ggef ggeg ggeh ggfe ggff ggfg ggfh ggge gggf gggg gggh gghe gghf gghg gghh ghee ghef gheg gheh ghfe ghff ghfg ghfh ghge ghgf ghgg ghgh ghhe ghhf ghhg ghhh heee heef heeg heeh hefe heff hefg hefh hege hegf hegg hegh hehe hehf hehg hehh hfee hfef hfeg hfeh hffe hfff hffg hffh hfge hfgf hfgg hfgh hfhe hfhf hfhg hfhh hgee hgef hgeg hgeh hgfe hgff hgfg hgfh hgge hggf hggg hggh hghe hghf hghg hghh hhee hhef hheg hheh hhfe hhff hhfg hhfh hhge hhgf hhgg hhgh hhhe hhhf hhhg hhhh
Example: given the input ijkl
(or any combination thereof of the four characters i
,j
,k
,l
), there is an equal chance of outputting:
i j k l ii ij ik il ji jj jk jl ki kj kk kl li lj lk ll iii iij iik iil iji ijj ijk ijl iki ikj ikk ikl ili ilj ilk ill jii jij jik jil jji jjj jjk jjl jki jkj jkk jkl jli jlj jlk jll kii kij kik kil kji kjj kjk kjl kki kkj kkk kkl kli klj klk kll lii lij lik lil lji ljj ljk ljl lki lkj lkk lkl lli llj llk lll iiii iiij iiik iiil iiji iijj iijk iijl iiki iikj iikk iikl iili iilj iilk iill ijii ijij ijik ijil ijji ijjj ijjk ijjl ijki ijkj ijkk ijkl ijli ijlj ijlk ijll ikii ikij ikik ikil ikji ikjj ikjk ikjl ikki ikkj ikkk ikkl ikli iklj iklk ikll ilii ilij ilik ilil ilji iljj iljk iljl ilki ilkj ilkk ilkl illi illj illk illl jiii jiij jiik jiil jiji jijj jijk jijl jiki jikj jikk jikl jili jilj jilk jill jjii jjij jjik jjil jjji jjjj jjjk jjjl jjki jjkj jjkk jjkl jjli jjlj jjlk jjll jkii jkij jkik jkil jkji jkjj jkjk jkjl jkki jkkj jkkk jkkl jkli jklj jklk jkll jlii jlij jlik jlil jlji jljj jljk jljl jlki jlkj jlkk jlkl jlli jllj jllk jlll kiii kiij kiik kiil kiji kijj kijk kijl kiki kikj kikk kikl kili kilj kilk kill kjii kjij kjik kjil kjji kjjj kjjk kjjl kjki kjkj kjkk kjkl kjli kjlj kjlk kjll kkii kkij kkik kkil kkji kkjj kkjk kkjl kkki kkkj kkkk kkkl kkli kklj kklk kkll klii klij klik klil klji kljj kljk kljl klki klkj klkk klkl klli kllj kllk klll liii liij liik liil liji lijj lijk lijl liki likj likk likl lili lilj lilk lill ljii ljij ljik ljil ljji ljjj ljjk ljjl ljki ljkj ljkk ljkl ljli ljlj ljlk ljll lkii lkij lkik lkil lkji lkjj lkjk lkjl lkki lkkj lkkk lkkl lkli lklj lklk lkll llii llij llik llil llji lljj lljk lljl llki llkj llkk llkl llli lllj lllk llll
This by definition also means that every indiviual character within the string too has equal probability. All characters in the input are distinct.
code-golf string random
$endgroup$
$begingroup$
Comments are not for extended discussion; this conversation has been moved to chat.
$endgroup$
– DJMcMayhem♦
Apr 5 at 22:42
1
$begingroup$
@Flog Edoc: (2) There is a real difference between 'will contain at least two distinct characters' and 'all characters will be distinct'. Which do you intend?
$endgroup$
– Chas Brown
Apr 6 at 5:35
4
$begingroup$
This challenge seems to have been significantly changed from what was initially posted, making it a completely different challenge and invalidating the existing solutions. That's an automatic-1
from me and the reason I've VTCed as unclear.
$endgroup$
– Shaggy
Apr 7 at 11:14
2
$begingroup$
@Shaggy As it stands now, many edge cases have in fact been resolved, so it is far from unclear as compared to where it started. Right now, I think it's pretty understandable what the requirements are. Other than exact formulaic assumptions (which may affect existing answers and may as well be a different question entirely), I'm not sure what you're looking to do.
$endgroup$
– Flog Edoc
Apr 8 at 16:33
1
$begingroup$
Your three examples are all essentially the same. I suggest showing the expected output for: the empty string (if applicable — I suggest you rule it out), inputx
, inputxy
, inputxxy
, inputzzz
.
$endgroup$
– Lynn
Apr 10 at 0:51
|
show 14 more comments
$begingroup$
Given an input string, output at random the unique combinations with repetition of the characters in the input string, from length 1 up to the length of the input string, with an equal chance of each one occurring.
Example: given the input abcd
(or any combination thereof of the four characters a
,b
,c
,d
) , there is an equal chance of outputting:
a b c d aa ab ac ad ba bb bc bd ca cb cc cd da db dc dd aaa aab aac aad aba abb abc abd aca acb acc acd ada adb adc add baa bab bac bad bba bbb bbc bbd bca bcb bcc bcd bda bdb bdc bdd caa cab cac cad cba cbb cbc cbd cca ccb ccc ccd cda cdb cdc cdd daa dab dac dad dba dbb dbc dbd dca dcb dcc dcd dda ddb ddc ddd aaaa aaab aaac aaad aaba aabb aabc aabd aaca aacb aacc aacd aada aadb aadc aadd abaa abab abac abad abba abbb abbc abbd abca abcb abcc abcd abda abdb abdc abdd acaa acab acac acad acba acbb acbc acbd acca accb accc accd acda acdb acdc acdd adaa adab adac adad adba adbb adbc adbd adca adcb adcc adcd adda addb addc addd baaa baab baac baad baba babb babc babd baca bacb bacc bacd bada badb badc badd bbaa bbab bbac bbad bbba bbbb bbbc bbbd bbca bbcb bbcc bbcd bbda bbdb bbdc bbdd bcaa bcab bcac bcad bcba bcbb bcbc bcbd bcca bccb bccc bccd bcda bcdb bcdc bcdd bdaa bdab bdac bdad bdba bdbb bdbc bdbd bdca bdcb bdcc bdcd bdda bddb bddc bddd caaa caab caac caad caba cabb cabc cabd caca cacb cacc cacd cada cadb cadc cadd cbaa cbab cbac cbad cbba cbbb cbbc cbbd cbca cbcb cbcc cbcd cbda cbdb cbdc cbdd ccaa ccab ccac ccad ccba ccbb ccbc ccbd ccca cccb cccc cccd ccda ccdb ccdc ccdd cdaa cdab cdac cdad cdba cdbb cdbc cdbd cdca cdcb cdcc cdcd cdda cddb cddc cddd daaa daab daac daad daba dabb dabc dabd daca dacb dacc dacd dada dadb dadc dadd dbaa dbab dbac dbad dbba dbbb dbbc dbbd dbca dbcb dbcc dbcd dbda dbdb dbdc dbdd dcaa dcab dcac dcad dcba dcbb dcbc dcbd dcca dccb dccc dccd dcda dcdb dcdc dcdd ddaa ddab ddac ddad ddba ddbb ddbc ddbd ddca ddcb ddcc ddcd ddda dddb dddc dddd
Example: given the input efgh
(or any combination thereof of the four characters e
,f
,g
,h
), there is an equal chance of outputting:
e f g h ee ef eg eh fe ff fg fh ge gf gg gh he hf hg hh eee eef eeg eeh efe eff efg efh ege egf egg egh ehe ehf ehg ehh fee fef feg feh ffe fff ffg ffh fge fgf fgg fgh fhe fhf fhg fhh gee gef geg geh gfe gff gfg gfh gge ggf ggg ggh ghe ghf ghg ghh hee hef heg heh hfe hff hfg hfh hge hgf hgg hgh hhe hhf hhg hhh eeee eeef eeeg eeeh eefe eeff eefg eefh eege eegf eegg eegh eehe eehf eehg eehh efee efef efeg efeh effe efff effg effh efge efgf efgg efgh efhe efhf efhg efhh egee egef egeg egeh egfe egff egfg egfh egge eggf eggg eggh eghe eghf eghg eghh ehee ehef eheg eheh ehfe ehff ehfg ehfh ehge ehgf ehgg ehgh ehhe ehhf ehhg ehhh feee feef feeg feeh fefe feff fefg fefh fege fegf fegg fegh fehe fehf fehg fehh ffee ffef ffeg ffeh fffe ffff fffg fffh ffge ffgf ffgg ffgh ffhe ffhf ffhg ffhh fgee fgef fgeg fgeh fgfe fgff fgfg fgfh fgge fggf fggg fggh fghe fghf fghg fghh fhee fhef fheg fheh fhfe fhff fhfg fhfh fhge fhgf fhgg fhgh fhhe fhhf fhhg fhhh geee geef geeg geeh gefe geff gefg gefh gege gegf gegg gegh gehe gehf gehg gehh gfee gfef gfeg gfeh gffe gfff gffg gffh gfge gfgf gfgg gfgh gfhe gfhf gfhg gfhh ggee ggef ggeg ggeh ggfe ggff ggfg ggfh ggge gggf gggg gggh gghe gghf gghg gghh ghee ghef gheg gheh ghfe ghff ghfg ghfh ghge ghgf ghgg ghgh ghhe ghhf ghhg ghhh heee heef heeg heeh hefe heff hefg hefh hege hegf hegg hegh hehe hehf hehg hehh hfee hfef hfeg hfeh hffe hfff hffg hffh hfge hfgf hfgg hfgh hfhe hfhf hfhg hfhh hgee hgef hgeg hgeh hgfe hgff hgfg hgfh hgge hggf hggg hggh hghe hghf hghg hghh hhee hhef hheg hheh hhfe hhff hhfg hhfh hhge hhgf hhgg hhgh hhhe hhhf hhhg hhhh
Example: given the input ijkl
(or any combination thereof of the four characters i
,j
,k
,l
), there is an equal chance of outputting:
i j k l ii ij ik il ji jj jk jl ki kj kk kl li lj lk ll iii iij iik iil iji ijj ijk ijl iki ikj ikk ikl ili ilj ilk ill jii jij jik jil jji jjj jjk jjl jki jkj jkk jkl jli jlj jlk jll kii kij kik kil kji kjj kjk kjl kki kkj kkk kkl kli klj klk kll lii lij lik lil lji ljj ljk ljl lki lkj lkk lkl lli llj llk lll iiii iiij iiik iiil iiji iijj iijk iijl iiki iikj iikk iikl iili iilj iilk iill ijii ijij ijik ijil ijji ijjj ijjk ijjl ijki ijkj ijkk ijkl ijli ijlj ijlk ijll ikii ikij ikik ikil ikji ikjj ikjk ikjl ikki ikkj ikkk ikkl ikli iklj iklk ikll ilii ilij ilik ilil ilji iljj iljk iljl ilki ilkj ilkk ilkl illi illj illk illl jiii jiij jiik jiil jiji jijj jijk jijl jiki jikj jikk jikl jili jilj jilk jill jjii jjij jjik jjil jjji jjjj jjjk jjjl jjki jjkj jjkk jjkl jjli jjlj jjlk jjll jkii jkij jkik jkil jkji jkjj jkjk jkjl jkki jkkj jkkk jkkl jkli jklj jklk jkll jlii jlij jlik jlil jlji jljj jljk jljl jlki jlkj jlkk jlkl jlli jllj jllk jlll kiii kiij kiik kiil kiji kijj kijk kijl kiki kikj kikk kikl kili kilj kilk kill kjii kjij kjik kjil kjji kjjj kjjk kjjl kjki kjkj kjkk kjkl kjli kjlj kjlk kjll kkii kkij kkik kkil kkji kkjj kkjk kkjl kkki kkkj kkkk kkkl kkli kklj kklk kkll klii klij klik klil klji kljj kljk kljl klki klkj klkk klkl klli kllj kllk klll liii liij liik liil liji lijj lijk lijl liki likj likk likl lili lilj lilk lill ljii ljij ljik ljil ljji ljjj ljjk ljjl ljki ljkj ljkk ljkl ljli ljlj ljlk ljll lkii lkij lkik lkil lkji lkjj lkjk lkjl lkki lkkj lkkk lkkl lkli lklj lklk lkll llii llij llik llil llji lljj lljk lljl llki llkj llkk llkl llli lllj lllk llll
This by definition also means that every indiviual character within the string too has equal probability. All characters in the input are distinct.
code-golf string random
$endgroup$
Given an input string, output at random the unique combinations with repetition of the characters in the input string, from length 1 up to the length of the input string, with an equal chance of each one occurring.
Example: given the input abcd
(or any combination thereof of the four characters a
,b
,c
,d
) , there is an equal chance of outputting:
a b c d aa ab ac ad ba bb bc bd ca cb cc cd da db dc dd aaa aab aac aad aba abb abc abd aca acb acc acd ada adb adc add baa bab bac bad bba bbb bbc bbd bca bcb bcc bcd bda bdb bdc bdd caa cab cac cad cba cbb cbc cbd cca ccb ccc ccd cda cdb cdc cdd daa dab dac dad dba dbb dbc dbd dca dcb dcc dcd dda ddb ddc ddd aaaa aaab aaac aaad aaba aabb aabc aabd aaca aacb aacc aacd aada aadb aadc aadd abaa abab abac abad abba abbb abbc abbd abca abcb abcc abcd abda abdb abdc abdd acaa acab acac acad acba acbb acbc acbd acca accb accc accd acda acdb acdc acdd adaa adab adac adad adba adbb adbc adbd adca adcb adcc adcd adda addb addc addd baaa baab baac baad baba babb babc babd baca bacb bacc bacd bada badb badc badd bbaa bbab bbac bbad bbba bbbb bbbc bbbd bbca bbcb bbcc bbcd bbda bbdb bbdc bbdd bcaa bcab bcac bcad bcba bcbb bcbc bcbd bcca bccb bccc bccd bcda bcdb bcdc bcdd bdaa bdab bdac bdad bdba bdbb bdbc bdbd bdca bdcb bdcc bdcd bdda bddb bddc bddd caaa caab caac caad caba cabb cabc cabd caca cacb cacc cacd cada cadb cadc cadd cbaa cbab cbac cbad cbba cbbb cbbc cbbd cbca cbcb cbcc cbcd cbda cbdb cbdc cbdd ccaa ccab ccac ccad ccba ccbb ccbc ccbd ccca cccb cccc cccd ccda ccdb ccdc ccdd cdaa cdab cdac cdad cdba cdbb cdbc cdbd cdca cdcb cdcc cdcd cdda cddb cddc cddd daaa daab daac daad daba dabb dabc dabd daca dacb dacc dacd dada dadb dadc dadd dbaa dbab dbac dbad dbba dbbb dbbc dbbd dbca dbcb dbcc dbcd dbda dbdb dbdc dbdd dcaa dcab dcac dcad dcba dcbb dcbc dcbd dcca dccb dccc dccd dcda dcdb dcdc dcdd ddaa ddab ddac ddad ddba ddbb ddbc ddbd ddca ddcb ddcc ddcd ddda dddb dddc dddd
Example: given the input efgh
(or any combination thereof of the four characters e
,f
,g
,h
), there is an equal chance of outputting:
e f g h ee ef eg eh fe ff fg fh ge gf gg gh he hf hg hh eee eef eeg eeh efe eff efg efh ege egf egg egh ehe ehf ehg ehh fee fef feg feh ffe fff ffg ffh fge fgf fgg fgh fhe fhf fhg fhh gee gef geg geh gfe gff gfg gfh gge ggf ggg ggh ghe ghf ghg ghh hee hef heg heh hfe hff hfg hfh hge hgf hgg hgh hhe hhf hhg hhh eeee eeef eeeg eeeh eefe eeff eefg eefh eege eegf eegg eegh eehe eehf eehg eehh efee efef efeg efeh effe efff effg effh efge efgf efgg efgh efhe efhf efhg efhh egee egef egeg egeh egfe egff egfg egfh egge eggf eggg eggh eghe eghf eghg eghh ehee ehef eheg eheh ehfe ehff ehfg ehfh ehge ehgf ehgg ehgh ehhe ehhf ehhg ehhh feee feef feeg feeh fefe feff fefg fefh fege fegf fegg fegh fehe fehf fehg fehh ffee ffef ffeg ffeh fffe ffff fffg fffh ffge ffgf ffgg ffgh ffhe ffhf ffhg ffhh fgee fgef fgeg fgeh fgfe fgff fgfg fgfh fgge fggf fggg fggh fghe fghf fghg fghh fhee fhef fheg fheh fhfe fhff fhfg fhfh fhge fhgf fhgg fhgh fhhe fhhf fhhg fhhh geee geef geeg geeh gefe geff gefg gefh gege gegf gegg gegh gehe gehf gehg gehh gfee gfef gfeg gfeh gffe gfff gffg gffh gfge gfgf gfgg gfgh gfhe gfhf gfhg gfhh ggee ggef ggeg ggeh ggfe ggff ggfg ggfh ggge gggf gggg gggh gghe gghf gghg gghh ghee ghef gheg gheh ghfe ghff ghfg ghfh ghge ghgf ghgg ghgh ghhe ghhf ghhg ghhh heee heef heeg heeh hefe heff hefg hefh hege hegf hegg hegh hehe hehf hehg hehh hfee hfef hfeg hfeh hffe hfff hffg hffh hfge hfgf hfgg hfgh hfhe hfhf hfhg hfhh hgee hgef hgeg hgeh hgfe hgff hgfg hgfh hgge hggf hggg hggh hghe hghf hghg hghh hhee hhef hheg hheh hhfe hhff hhfg hhfh hhge hhgf hhgg hhgh hhhe hhhf hhhg hhhh
Example: given the input ijkl
(or any combination thereof of the four characters i
,j
,k
,l
), there is an equal chance of outputting:
i j k l ii ij ik il ji jj jk jl ki kj kk kl li lj lk ll iii iij iik iil iji ijj ijk ijl iki ikj ikk ikl ili ilj ilk ill jii jij jik jil jji jjj jjk jjl jki jkj jkk jkl jli jlj jlk jll kii kij kik kil kji kjj kjk kjl kki kkj kkk kkl kli klj klk kll lii lij lik lil lji ljj ljk ljl lki lkj lkk lkl lli llj llk lll iiii iiij iiik iiil iiji iijj iijk iijl iiki iikj iikk iikl iili iilj iilk iill ijii ijij ijik ijil ijji ijjj ijjk ijjl ijki ijkj ijkk ijkl ijli ijlj ijlk ijll ikii ikij ikik ikil ikji ikjj ikjk ikjl ikki ikkj ikkk ikkl ikli iklj iklk ikll ilii ilij ilik ilil ilji iljj iljk iljl ilki ilkj ilkk ilkl illi illj illk illl jiii jiij jiik jiil jiji jijj jijk jijl jiki jikj jikk jikl jili jilj jilk jill jjii jjij jjik jjil jjji jjjj jjjk jjjl jjki jjkj jjkk jjkl jjli jjlj jjlk jjll jkii jkij jkik jkil jkji jkjj jkjk jkjl jkki jkkj jkkk jkkl jkli jklj jklk jkll jlii jlij jlik jlil jlji jljj jljk jljl jlki jlkj jlkk jlkl jlli jllj jllk jlll kiii kiij kiik kiil kiji kijj kijk kijl kiki kikj kikk kikl kili kilj kilk kill kjii kjij kjik kjil kjji kjjj kjjk kjjl kjki kjkj kjkk kjkl kjli kjlj kjlk kjll kkii kkij kkik kkil kkji kkjj kkjk kkjl kkki kkkj kkkk kkkl kkli kklj kklk kkll klii klij klik klil klji kljj kljk kljl klki klkj klkk klkl klli kllj kllk klll liii liij liik liil liji lijj lijk lijl liki likj likk likl lili lilj lilk lill ljii ljij ljik ljil ljji ljjj ljjk ljjl ljki ljkj ljkk ljkl ljli ljlj ljlk ljll lkii lkij lkik lkil lkji lkjj lkjk lkjl lkki lkkj lkkk lkkl lkli lklj lklk lkll llii llij llik llil llji lljj lljk lljl llki llkj llkk llkl llli lllj lllk llll
This by definition also means that every indiviual character within the string too has equal probability. All characters in the input are distinct.
code-golf string random
code-golf string random
edited Apr 8 at 13:31
Flog Edoc
asked Apr 5 at 18:36
Flog EdocFlog Edoc
413
413
$begingroup$
Comments are not for extended discussion; this conversation has been moved to chat.
$endgroup$
– DJMcMayhem♦
Apr 5 at 22:42
1
$begingroup$
@Flog Edoc: (2) There is a real difference between 'will contain at least two distinct characters' and 'all characters will be distinct'. Which do you intend?
$endgroup$
– Chas Brown
Apr 6 at 5:35
4
$begingroup$
This challenge seems to have been significantly changed from what was initially posted, making it a completely different challenge and invalidating the existing solutions. That's an automatic-1
from me and the reason I've VTCed as unclear.
$endgroup$
– Shaggy
Apr 7 at 11:14
2
$begingroup$
@Shaggy As it stands now, many edge cases have in fact been resolved, so it is far from unclear as compared to where it started. Right now, I think it's pretty understandable what the requirements are. Other than exact formulaic assumptions (which may affect existing answers and may as well be a different question entirely), I'm not sure what you're looking to do.
$endgroup$
– Flog Edoc
Apr 8 at 16:33
1
$begingroup$
Your three examples are all essentially the same. I suggest showing the expected output for: the empty string (if applicable — I suggest you rule it out), inputx
, inputxy
, inputxxy
, inputzzz
.
$endgroup$
– Lynn
Apr 10 at 0:51
|
show 14 more comments
$begingroup$
Comments are not for extended discussion; this conversation has been moved to chat.
$endgroup$
– DJMcMayhem♦
Apr 5 at 22:42
1
$begingroup$
@Flog Edoc: (2) There is a real difference between 'will contain at least two distinct characters' and 'all characters will be distinct'. Which do you intend?
$endgroup$
– Chas Brown
Apr 6 at 5:35
4
$begingroup$
This challenge seems to have been significantly changed from what was initially posted, making it a completely different challenge and invalidating the existing solutions. That's an automatic-1
from me and the reason I've VTCed as unclear.
$endgroup$
– Shaggy
Apr 7 at 11:14
2
$begingroup$
@Shaggy As it stands now, many edge cases have in fact been resolved, so it is far from unclear as compared to where it started. Right now, I think it's pretty understandable what the requirements are. Other than exact formulaic assumptions (which may affect existing answers and may as well be a different question entirely), I'm not sure what you're looking to do.
$endgroup$
– Flog Edoc
Apr 8 at 16:33
1
$begingroup$
Your three examples are all essentially the same. I suggest showing the expected output for: the empty string (if applicable — I suggest you rule it out), inputx
, inputxy
, inputxxy
, inputzzz
.
$endgroup$
– Lynn
Apr 10 at 0:51
$begingroup$
Comments are not for extended discussion; this conversation has been moved to chat.
$endgroup$
– DJMcMayhem♦
Apr 5 at 22:42
$begingroup$
Comments are not for extended discussion; this conversation has been moved to chat.
$endgroup$
– DJMcMayhem♦
Apr 5 at 22:42
1
1
$begingroup$
@Flog Edoc: (2) There is a real difference between 'will contain at least two distinct characters' and 'all characters will be distinct'. Which do you intend?
$endgroup$
– Chas Brown
Apr 6 at 5:35
$begingroup$
@Flog Edoc: (2) There is a real difference between 'will contain at least two distinct characters' and 'all characters will be distinct'. Which do you intend?
$endgroup$
– Chas Brown
Apr 6 at 5:35
4
4
$begingroup$
This challenge seems to have been significantly changed from what was initially posted, making it a completely different challenge and invalidating the existing solutions. That's an automatic
-1
from me and the reason I've VTCed as unclear.$endgroup$
– Shaggy
Apr 7 at 11:14
$begingroup$
This challenge seems to have been significantly changed from what was initially posted, making it a completely different challenge and invalidating the existing solutions. That's an automatic
-1
from me and the reason I've VTCed as unclear.$endgroup$
– Shaggy
Apr 7 at 11:14
2
2
$begingroup$
@Shaggy As it stands now, many edge cases have in fact been resolved, so it is far from unclear as compared to where it started. Right now, I think it's pretty understandable what the requirements are. Other than exact formulaic assumptions (which may affect existing answers and may as well be a different question entirely), I'm not sure what you're looking to do.
$endgroup$
– Flog Edoc
Apr 8 at 16:33
$begingroup$
@Shaggy As it stands now, many edge cases have in fact been resolved, so it is far from unclear as compared to where it started. Right now, I think it's pretty understandable what the requirements are. Other than exact formulaic assumptions (which may affect existing answers and may as well be a different question entirely), I'm not sure what you're looking to do.
$endgroup$
– Flog Edoc
Apr 8 at 16:33
1
1
$begingroup$
Your three examples are all essentially the same. I suggest showing the expected output for: the empty string (if applicable — I suggest you rule it out), input
x
, input xy
, input xxy
, input zzz
.$endgroup$
– Lynn
Apr 10 at 0:51
$begingroup$
Your three examples are all essentially the same. I suggest showing the expected output for: the empty string (if applicable — I suggest you rule it out), input
x
, input xy
, input xxy
, input zzz
.$endgroup$
– Lynn
Apr 10 at 0:51
|
show 14 more comments
18 Answers
18
active
oldest
votes
$begingroup$
Jelly, 10 5 bytes
ṗJẎQX
A monadic Link accepting a list of characters which yields a list of characters.
Try it online!
How?
ṗJẎQX - Link list of characters e.g. aabc
J - range of length [1,2,3,4]
ṗ - Cartesian power (vectorises) [[a,a,b,c],[aa,aa,ab,ac,aa,aa,ab,ac,ba,ba,bb,bc,ca,ca,cb,cc],[aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,baa,baa,bab,bac,baa,baa,bab,bac,bba,bba,bbb,bbc,bca,bca,bcb,bcc,caa,caa,cab,cac,caa,caa,cab,cac,cba,cba,cbb,cbc,cca,cca,ccb,ccc],[aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,bbaa,bbaa,bbab,bbac,bbaa,bbaa,bbab,bbac,bbba,bbba,bbbb,bbbc,bbca,bbca,bbcb,bbcc,bcaa,bcaa,bcab,bcac,bcaa,bcaa,bcab,bcac,bcba,bcba,bcbb,bcbc,bcca,bcca,bccb,bccc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,cbaa,cbaa,cbab,cbac,cbaa,cbaa,cbab,cbac,cbba,cbba,cbbb,cbbc,cbca,cbca,cbcb,cbcc,ccaa,ccaa,ccab,ccac,ccaa,ccaa,ccab,ccac,ccba,ccba,ccbb,ccbc,ccca,ccca,cccb,cccc]
Ẏ - tighten [a,a,b,c,aa,aa,ab,ac,aa,aa,ab,ac,ba,ba,bb,bc,ca,ca,cb,cc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,baa,baa,bab,bac,baa,baa,bab,bac,bba,bba,bbb,bbc,bca,bca,bcb,bcc,caa,caa,cab,cac,caa,caa,cab,cac,cba,cba,cbb,cbc,cca,cca,ccb,ccc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,bbaa,bbaa,bbab,bbac,bbaa,bbaa,bbab,bbac,bbba,bbba,bbbb,bbbc,bbca,bbca,bbcb,bbcc,bcaa,bcaa,bcab,bcac,bcaa,bcaa,bcab,bcac,bcba,bcba,bcbb,bcbc,bcca,bcca,bccb,bccc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,cbaa,cbaa,cbab,cbac,cbaa,cbaa,cbab,cbac,cbba,cbba,cbbb,cbbc,cbca,cbca,cbcb,cbcc,ccaa,ccaa,ccab,ccac,ccaa,ccaa,ccab,ccac,ccba,ccba,ccbb,ccbc,ccca,ccca,cccb,cccc]
Q - de-duplicate [a,b,c,aa,ab,ac,ba,bb,bc,ca,cb,cc,aaa,aab,aac,aba,abb,abc,aca,acb,acc,baa,bab,bac,bba,bbb,bbc,bca,bcb,bcc,caa,cab,cac,cba,cbb,cbc,cca,ccb,ccc,aaaa,aaab,aaac,aaba,aabb,aabc,aaca,aacb,aacc,abaa,abab,abac,abba,abbb,abbc,abca,abcb,abcc,acaa,acab,acac,acba,acbb,acbc,acca,accb,accc,baaa,baab,baac,baba,babb,babc,baca,bacb,bacc,bbaa,bbab,bbac,bbba,bbbb,bbbc,bbca,bbcb,bbcc,bcaa,bcab,bcac,bcba,bcbb,bcbc,bcca,bccb,bccc,caaa,caab,caac,caba,cabb,cabc,caca,cacb,cacc,cbaa,cbab,cbac,cbba,cbbb,cbbc,cbca,cbcb,cbcc,ccaa,ccab,ccac,ccba,ccbb,ccbc,ccca,cccb,cccc]
X - uniform random choice
$endgroup$
add a comment |
$begingroup$
05AB1E, 6 bytes
ā€ã˜ÙΩ
Try it online!
Explanation
ā # push [1 ... len(input)]
ۋ # apply repeated cartesian product on each and input
˜ # flatten
Ù # remove duplicates
Ω # pick random string
$endgroup$
add a comment |
$begingroup$
Perl 6, 35 33 28 bytes
-2 bytes thanks to nwellnhof
{[~] roll .rand+1,$_}o*.comb
Try it online!
This challenge keeps changing a lot.
Explanation:
{ }o*.comb # Anonymous code block turning input into chars
$_ # From the input
.rand+1, # Take a random number from 1 to length of list
roll # Of characters
[~] # And join them into a string
$endgroup$
$begingroup$
33 bytes
$endgroup$
– nwellnhof
Apr 7 at 9:05
add a comment |
$begingroup$
Brachylog, 8 bytes
⊇ᶠbṛ;?ṛw
Try it online!
Prints output directly instead of constraining the output variable, because for some reason this actually fails without the w
at the end. Originally this used ≠
and the ∈&
backtracking hack I used to implement bogosort, but I realized that it would be far saner to just use b
instead.
w Print
ṛ a random one of
? the input
; or
ṛ a random element of
ᶠ every
⊇ sublist of
the input
b except the first one (which would be the input).
$endgroup$
add a comment |
$begingroup$
Python 2, 111 121 bytes
lambda s:choice(g(list(set(s)),len(s)))
from random import *
g=lambda s,n:s+(n>1and[c+k for k in g(s,n-1)for c in s]or)
Try it online!
Takes any iterable (list, set, string, etc.) of either distinct characters or non-distinct characters as input. Outputs a non-empty string with the required distribution.
g
is a recursive function to generate a list of compliant strings.
$endgroup$
add a comment |
$begingroup$
R, 72 64 bytes
-8 bytes thanks to Giuseppe.
S=sample;S(t<-unique(s<-scan(,"")),S(n<-seq(s),p=length(t)^n),T)
Try it online!
Input and output are vectors of characters. All possible outputs have equal probability.
Explanation (ungolfed version):
s<-scan(,"") # takes input as vector of characters
n<-length(s)
t<-unique(s)
sample(t, # sample uniformly at random from the list of unique characters
sample(n,p=length(t)^(1:n)), # with length k in 1..n chosen randomly such that P[k] is proportional to length(t)^k
T) # the sampling is with replacement
$endgroup$
1
$begingroup$
64 bytes
$endgroup$
– Giuseppe
Apr 10 at 1:36
$begingroup$
@Giuseppe Thanks!
$endgroup$
– Robin Ryder
Apr 10 at 5:52
add a comment |
$begingroup$
Wolfram Language (Mathematica), 41 bytes
""<>#&@*RandomChoice@*Subsets@*Characters
Try it online!
$endgroup$
$begingroup$
Wha is this part?""<>#&@
$endgroup$
– Jonah
Apr 6 at 21:30
1
$begingroup$
@Jonah<>
isStringJoin
;""<>#&
is an anonymous function that joins""
with its argument (and is much shorter thanStringJoin
).@*
composes functions.
$endgroup$
– attinat
Apr 7 at 18:19
add a comment |
$begingroup$
Java 131 bytes
void k(String a){a.chars().forEach(v->{int z=(int)(Math.random()*(a.length()+1));if(z<a.length())System.out.print(a.charAt(z));});}
$endgroup$
$begingroup$
Nice approach! Since you're using a Java 8+ stream, why not changevoid k(String a){a.chars()..;}
toa->a.chars()..
as well? Also,int z=(int)(Math.random()*(a.length()+1));if(z<a.length())
can beint l=a.length(),z=l+1;z*=Math.random();if(z<l)
to save some more bytes. In total it then becomes 104 bytes
$endgroup$
– Kevin Cruijssen
2 days ago
add a comment |
$begingroup$
Pyth - 7 bytes
Os^LQSl
O Random choice
s Collapse list
^L Map cartesian power
Q Input
S 1-indexed range
l Length
(Q implicit) Input
Try it online.
Try it online without random pick to see all possible options.
$endgroup$
add a comment |
$begingroup$
C (gcc), 68 65 bytes
f(s,i)char*s;{for(;i<strlen(s);i++)rand()&1?putc(s[i],stdout):0;}
Try it online!
$endgroup$
$begingroup$
I don't think this is correct anymore
$endgroup$
– Jo King
Apr 6 at 3:02
1
$begingroup$
If the op changed the rules that's a pity.
$endgroup$
– Natural Number Guy
Apr 6 at 11:45
add a comment |
$begingroup$
Charcoal, 34 bytes
≔Φθ⁼κ⌕θιη≔⊕‽ΣEθXLη⊕κζW櫧ηζ≔÷⊖ζLηζ
Try it online! Link is to verbose version of code. Explanation:
≔Φθ⁼κ⌕θιη
Extract the unique characters of the input. Let's call the number of unique characters n
.
≔⊕‽ΣEθXLη⊕κζ
Calculate the number of combinations for each possible length and take the sum. Then, pick a random number between 1 and this number (inclusive). This ensures that all combinations are equally likely (within the accuracy of the random number generator).
W櫧ηζ≔÷⊖ζLηζ
Convert the number into bijective base n
, using the unique characters as the digits.
$endgroup$
add a comment |
$begingroup$
MATLAB / Octave, 110 bytes
Choose a random permutation of a subset of the input letters (with repetitions), whose random length is based on the probability of generating a word with that length.
@(a)a(randi(numel(a),[find(cumsum(numel(a).^[1:numel(a)])>=randi(sum(numel(a).^[1:numel(a)])),1,'first'),1]));
Try it online!
$endgroup$
add a comment |
$begingroup$
T-SQL, 222 bytes
This creates all combinations of each unique character with recursive sql, then picks a random row from the distinct combinations.
DECLARE @ varchar(max)='T-SQL';
WITH C as(SELECT DISTINCT substring(@,number+1,1)x
FROM spt_values
WHERE'P'=type and len(@)>number),D
as(SELECT x y
FROM c UNION ALL
SELECT y+x
FROM C JOIN D
ON len(y)<len(@))SELECT top 1*FROM D
GROUP BY y
ORDER BY newid()
Note the online version will always give the same result unlike MS-SQL Studio Management. This is because newid() always returns the same value in the online testing. This should work in Studio Management.
Try it online ungolfed version
$endgroup$
add a comment |
$begingroup$
Python 2, 124 bytes
lambda s:g(list(set(s)),len(s))
from random import*
g=lambda s,n:choice(s)+(random()*~-len(s)**(n)>~-len(s)and g(s,n-1)or'')
Try it online!
A different approach: instead of first constructing a list of all compliant strings and choosing one at random, this approach randomly decides to continue extending the string or stopping.
This again has to deal with the exacting input requirements, at a cost of 31 bytes; but the function of interest g
takes a list s
of characters to be used, and an integer n
which is the maximum length of the returned string.
As an example, consider the set of characters ['a','b'] and suppose we want to generate random strings of length 1, 2, or 3.
Then if we randomly choose 'a' as the starting character, the possible strings which could be returned are:
a
aa
aaa
aab
ab
aba
abb
shown above in 'tree' form, which is a total of 1 + 2 + 2*2 = 2^0 + 2^1 + 2^2 = 7
strings. So if we generate a string a
; then 1/7
of the time we should stop and return 'a'
, and 6/7
of the time we should add further to the string - then the distribution will be uniform in the way desired.
More generally, if n
is maximum length of the string and x
is the number of characters in the set, then the number of strings starting with some given character is going to be:
$$h(n)=sum_{i=0}^{n-1} x^i$$
$$h(n)=1+x+x^2+x^3...+x^{n-1}$$
For x>1
(guaranteed by OP's rule 'at least two distinct...'), we have:
$$h(n)(x-1)=(1+x+x^2+x^3...+x^{n-1})(x-1)=x^n-1$$
$$h(n)=frac{x^n-1}{x-1}$$
So to decide whether we should continue extending our string, let p
be a random number in [0,1)
; then we should recurse only if any of these equivalent statements are true:
$$p>frac{1}{h(n)}$$
$$p>frac{x-1}{x^n-1}$$
$$p(x^n-1)>x-1$$
of which g
is the golfed implementation.
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 126 bytes
a=>{var s=new Random();int d=a.Length;return Enumerable.Range(1,s.Next(1,d)).Select(x=>a.Distinct().ToList()[s.Next(0,d-1)]);}
Try it online!
$endgroup$
add a comment |
$begingroup$
Forth (gforth), 68 bytes
include random.fs
: f dup random 1+ 0 do 2dup random + 1 type loop ;
Try it online!
Explanation
- Get a random number between 1 and string-length
- Loop that many times
- For each iteration
- Get a random number between 0 and string length - 1
- Add that to string starting address and output the character at that address
Code Explanation
include random.fs include the library file needed to generate random numbers
: f start a new word definition
dup duplicate the string-length
random 1+ get the length of the new string, make sure it starts from 1
0 do start a loop of that length
2dup duplicate the starting address and string length
random + get a number from 0 to string-length and add it to the address
1 type output the character at that address
loop end the loop
; end the word definition
$endgroup$
add a comment |
$begingroup$
J, 27 bytes
[:(?@#{])@;[:,@{&.>#<@#"{<
Try it online!
standard formatting
[: (?@# { ])@; [: ,@{&.> # <@#"1 _ <
explanation
Eg, for the string 'abc' we first use #<@#"{<
to create:
┌─────┬─────────┬─────────────┐
│┌───┐│┌───┬───┐│┌───┬───┬───┐│
││abc│││abc│abc│││abc│abc│abc││
│└───┘│└───┴───┘│└───┴───┴───┘│
└─────┴─────────┴─────────────┘
We then take the cartesian product of each of these {
, flatten the results, and finally remove the outer boxing ;
.
(?@#{])@
takes a random result from that list.
$endgroup$
$begingroup$
TIO will return the same result every time, but in a normal J REPL it will be random. sounds like a bug to report, random works for Python 3 TIO...
$endgroup$
– Artemis Fowl
Apr 9 at 0:16
$begingroup$
J will start each session with the same seed for its RNG. If you keep running the same verb in a single session, you'll get different answers. But if you start your session over, the same sequence will occur. TIO is like a fresh J session. So it's not exactly a bug -- at least I wouldn't call it one.
$endgroup$
– Jonah
Apr 9 at 0:57
$begingroup$
Bug, maybe not. Improvable feature, quite possibly. As someone who's never used J, I'll leave it up to you to report the bug if you wish to.
$endgroup$
– Artemis Fowl
Apr 9 at 1:03
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
Apr 10 at 0:06
$begingroup$
The program now seems to output a list of values...
$endgroup$
– Artemis Fowl
2 days ago
|
show 2 more comments
$begingroup$
Haskell, 143 bytes
import Control.Monad
import System.Random
f a=do
i<-randomRIO(0,length c-1)
print$c!!i
where c=concat[replicateM x a|x <-[1..length a]]
Try it online!
New contributor
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "200"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182735%2fto-string-or-not-to-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
18 Answers
18
active
oldest
votes
18 Answers
18
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Jelly, 10 5 bytes
ṗJẎQX
A monadic Link accepting a list of characters which yields a list of characters.
Try it online!
How?
ṗJẎQX - Link list of characters e.g. aabc
J - range of length [1,2,3,4]
ṗ - Cartesian power (vectorises) [[a,a,b,c],[aa,aa,ab,ac,aa,aa,ab,ac,ba,ba,bb,bc,ca,ca,cb,cc],[aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,baa,baa,bab,bac,baa,baa,bab,bac,bba,bba,bbb,bbc,bca,bca,bcb,bcc,caa,caa,cab,cac,caa,caa,cab,cac,cba,cba,cbb,cbc,cca,cca,ccb,ccc],[aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,bbaa,bbaa,bbab,bbac,bbaa,bbaa,bbab,bbac,bbba,bbba,bbbb,bbbc,bbca,bbca,bbcb,bbcc,bcaa,bcaa,bcab,bcac,bcaa,bcaa,bcab,bcac,bcba,bcba,bcbb,bcbc,bcca,bcca,bccb,bccc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,cbaa,cbaa,cbab,cbac,cbaa,cbaa,cbab,cbac,cbba,cbba,cbbb,cbbc,cbca,cbca,cbcb,cbcc,ccaa,ccaa,ccab,ccac,ccaa,ccaa,ccab,ccac,ccba,ccba,ccbb,ccbc,ccca,ccca,cccb,cccc]
Ẏ - tighten [a,a,b,c,aa,aa,ab,ac,aa,aa,ab,ac,ba,ba,bb,bc,ca,ca,cb,cc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,baa,baa,bab,bac,baa,baa,bab,bac,bba,bba,bbb,bbc,bca,bca,bcb,bcc,caa,caa,cab,cac,caa,caa,cab,cac,cba,cba,cbb,cbc,cca,cca,ccb,ccc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,bbaa,bbaa,bbab,bbac,bbaa,bbaa,bbab,bbac,bbba,bbba,bbbb,bbbc,bbca,bbca,bbcb,bbcc,bcaa,bcaa,bcab,bcac,bcaa,bcaa,bcab,bcac,bcba,bcba,bcbb,bcbc,bcca,bcca,bccb,bccc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,cbaa,cbaa,cbab,cbac,cbaa,cbaa,cbab,cbac,cbba,cbba,cbbb,cbbc,cbca,cbca,cbcb,cbcc,ccaa,ccaa,ccab,ccac,ccaa,ccaa,ccab,ccac,ccba,ccba,ccbb,ccbc,ccca,ccca,cccb,cccc]
Q - de-duplicate [a,b,c,aa,ab,ac,ba,bb,bc,ca,cb,cc,aaa,aab,aac,aba,abb,abc,aca,acb,acc,baa,bab,bac,bba,bbb,bbc,bca,bcb,bcc,caa,cab,cac,cba,cbb,cbc,cca,ccb,ccc,aaaa,aaab,aaac,aaba,aabb,aabc,aaca,aacb,aacc,abaa,abab,abac,abba,abbb,abbc,abca,abcb,abcc,acaa,acab,acac,acba,acbb,acbc,acca,accb,accc,baaa,baab,baac,baba,babb,babc,baca,bacb,bacc,bbaa,bbab,bbac,bbba,bbbb,bbbc,bbca,bbcb,bbcc,bcaa,bcab,bcac,bcba,bcbb,bcbc,bcca,bccb,bccc,caaa,caab,caac,caba,cabb,cabc,caca,cacb,cacc,cbaa,cbab,cbac,cbba,cbbb,cbbc,cbca,cbcb,cbcc,ccaa,ccab,ccac,ccba,ccbb,ccbc,ccca,cccb,cccc]
X - uniform random choice
$endgroup$
add a comment |
$begingroup$
Jelly, 10 5 bytes
ṗJẎQX
A monadic Link accepting a list of characters which yields a list of characters.
Try it online!
How?
ṗJẎQX - Link list of characters e.g. aabc
J - range of length [1,2,3,4]
ṗ - Cartesian power (vectorises) [[a,a,b,c],[aa,aa,ab,ac,aa,aa,ab,ac,ba,ba,bb,bc,ca,ca,cb,cc],[aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,baa,baa,bab,bac,baa,baa,bab,bac,bba,bba,bbb,bbc,bca,bca,bcb,bcc,caa,caa,cab,cac,caa,caa,cab,cac,cba,cba,cbb,cbc,cca,cca,ccb,ccc],[aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,bbaa,bbaa,bbab,bbac,bbaa,bbaa,bbab,bbac,bbba,bbba,bbbb,bbbc,bbca,bbca,bbcb,bbcc,bcaa,bcaa,bcab,bcac,bcaa,bcaa,bcab,bcac,bcba,bcba,bcbb,bcbc,bcca,bcca,bccb,bccc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,cbaa,cbaa,cbab,cbac,cbaa,cbaa,cbab,cbac,cbba,cbba,cbbb,cbbc,cbca,cbca,cbcb,cbcc,ccaa,ccaa,ccab,ccac,ccaa,ccaa,ccab,ccac,ccba,ccba,ccbb,ccbc,ccca,ccca,cccb,cccc]
Ẏ - tighten [a,a,b,c,aa,aa,ab,ac,aa,aa,ab,ac,ba,ba,bb,bc,ca,ca,cb,cc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,baa,baa,bab,bac,baa,baa,bab,bac,bba,bba,bbb,bbc,bca,bca,bcb,bcc,caa,caa,cab,cac,caa,caa,cab,cac,cba,cba,cbb,cbc,cca,cca,ccb,ccc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,bbaa,bbaa,bbab,bbac,bbaa,bbaa,bbab,bbac,bbba,bbba,bbbb,bbbc,bbca,bbca,bbcb,bbcc,bcaa,bcaa,bcab,bcac,bcaa,bcaa,bcab,bcac,bcba,bcba,bcbb,bcbc,bcca,bcca,bccb,bccc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,cbaa,cbaa,cbab,cbac,cbaa,cbaa,cbab,cbac,cbba,cbba,cbbb,cbbc,cbca,cbca,cbcb,cbcc,ccaa,ccaa,ccab,ccac,ccaa,ccaa,ccab,ccac,ccba,ccba,ccbb,ccbc,ccca,ccca,cccb,cccc]
Q - de-duplicate [a,b,c,aa,ab,ac,ba,bb,bc,ca,cb,cc,aaa,aab,aac,aba,abb,abc,aca,acb,acc,baa,bab,bac,bba,bbb,bbc,bca,bcb,bcc,caa,cab,cac,cba,cbb,cbc,cca,ccb,ccc,aaaa,aaab,aaac,aaba,aabb,aabc,aaca,aacb,aacc,abaa,abab,abac,abba,abbb,abbc,abca,abcb,abcc,acaa,acab,acac,acba,acbb,acbc,acca,accb,accc,baaa,baab,baac,baba,babb,babc,baca,bacb,bacc,bbaa,bbab,bbac,bbba,bbbb,bbbc,bbca,bbcb,bbcc,bcaa,bcab,bcac,bcba,bcbb,bcbc,bcca,bccb,bccc,caaa,caab,caac,caba,cabb,cabc,caca,cacb,cacc,cbaa,cbab,cbac,cbba,cbbb,cbbc,cbca,cbcb,cbcc,ccaa,ccab,ccac,ccba,ccbb,ccbc,ccca,cccb,cccc]
X - uniform random choice
$endgroup$
add a comment |
$begingroup$
Jelly, 10 5 bytes
ṗJẎQX
A monadic Link accepting a list of characters which yields a list of characters.
Try it online!
How?
ṗJẎQX - Link list of characters e.g. aabc
J - range of length [1,2,3,4]
ṗ - Cartesian power (vectorises) [[a,a,b,c],[aa,aa,ab,ac,aa,aa,ab,ac,ba,ba,bb,bc,ca,ca,cb,cc],[aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,baa,baa,bab,bac,baa,baa,bab,bac,bba,bba,bbb,bbc,bca,bca,bcb,bcc,caa,caa,cab,cac,caa,caa,cab,cac,cba,cba,cbb,cbc,cca,cca,ccb,ccc],[aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,bbaa,bbaa,bbab,bbac,bbaa,bbaa,bbab,bbac,bbba,bbba,bbbb,bbbc,bbca,bbca,bbcb,bbcc,bcaa,bcaa,bcab,bcac,bcaa,bcaa,bcab,bcac,bcba,bcba,bcbb,bcbc,bcca,bcca,bccb,bccc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,cbaa,cbaa,cbab,cbac,cbaa,cbaa,cbab,cbac,cbba,cbba,cbbb,cbbc,cbca,cbca,cbcb,cbcc,ccaa,ccaa,ccab,ccac,ccaa,ccaa,ccab,ccac,ccba,ccba,ccbb,ccbc,ccca,ccca,cccb,cccc]
Ẏ - tighten [a,a,b,c,aa,aa,ab,ac,aa,aa,ab,ac,ba,ba,bb,bc,ca,ca,cb,cc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,baa,baa,bab,bac,baa,baa,bab,bac,bba,bba,bbb,bbc,bca,bca,bcb,bcc,caa,caa,cab,cac,caa,caa,cab,cac,cba,cba,cbb,cbc,cca,cca,ccb,ccc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,bbaa,bbaa,bbab,bbac,bbaa,bbaa,bbab,bbac,bbba,bbba,bbbb,bbbc,bbca,bbca,bbcb,bbcc,bcaa,bcaa,bcab,bcac,bcaa,bcaa,bcab,bcac,bcba,bcba,bcbb,bcbc,bcca,bcca,bccb,bccc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,cbaa,cbaa,cbab,cbac,cbaa,cbaa,cbab,cbac,cbba,cbba,cbbb,cbbc,cbca,cbca,cbcb,cbcc,ccaa,ccaa,ccab,ccac,ccaa,ccaa,ccab,ccac,ccba,ccba,ccbb,ccbc,ccca,ccca,cccb,cccc]
Q - de-duplicate [a,b,c,aa,ab,ac,ba,bb,bc,ca,cb,cc,aaa,aab,aac,aba,abb,abc,aca,acb,acc,baa,bab,bac,bba,bbb,bbc,bca,bcb,bcc,caa,cab,cac,cba,cbb,cbc,cca,ccb,ccc,aaaa,aaab,aaac,aaba,aabb,aabc,aaca,aacb,aacc,abaa,abab,abac,abba,abbb,abbc,abca,abcb,abcc,acaa,acab,acac,acba,acbb,acbc,acca,accb,accc,baaa,baab,baac,baba,babb,babc,baca,bacb,bacc,bbaa,bbab,bbac,bbba,bbbb,bbbc,bbca,bbcb,bbcc,bcaa,bcab,bcac,bcba,bcbb,bcbc,bcca,bccb,bccc,caaa,caab,caac,caba,cabb,cabc,caca,cacb,cacc,cbaa,cbab,cbac,cbba,cbbb,cbbc,cbca,cbcb,cbcc,ccaa,ccab,ccac,ccba,ccbb,ccbc,ccca,cccb,cccc]
X - uniform random choice
$endgroup$
Jelly, 10 5 bytes
ṗJẎQX
A monadic Link accepting a list of characters which yields a list of characters.
Try it online!
How?
ṗJẎQX - Link list of characters e.g. aabc
J - range of length [1,2,3,4]
ṗ - Cartesian power (vectorises) [[a,a,b,c],[aa,aa,ab,ac,aa,aa,ab,ac,ba,ba,bb,bc,ca,ca,cb,cc],[aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,baa,baa,bab,bac,baa,baa,bab,bac,bba,bba,bbb,bbc,bca,bca,bcb,bcc,caa,caa,cab,cac,caa,caa,cab,cac,cba,cba,cbb,cbc,cca,cca,ccb,ccc],[aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,bbaa,bbaa,bbab,bbac,bbaa,bbaa,bbab,bbac,bbba,bbba,bbbb,bbbc,bbca,bbca,bbcb,bbcc,bcaa,bcaa,bcab,bcac,bcaa,bcaa,bcab,bcac,bcba,bcba,bcbb,bcbc,bcca,bcca,bccb,bccc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,cbaa,cbaa,cbab,cbac,cbaa,cbaa,cbab,cbac,cbba,cbba,cbbb,cbbc,cbca,cbca,cbcb,cbcc,ccaa,ccaa,ccab,ccac,ccaa,ccaa,ccab,ccac,ccba,ccba,ccbb,ccbc,ccca,ccca,cccb,cccc]
Ẏ - tighten [a,a,b,c,aa,aa,ab,ac,aa,aa,ab,ac,ba,ba,bb,bc,ca,ca,cb,cc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,aaa,aaa,aab,aac,aaa,aaa,aab,aac,aba,aba,abb,abc,aca,aca,acb,acc,baa,baa,bab,bac,baa,baa,bab,bac,bba,bba,bbb,bbc,bca,bca,bcb,bcc,caa,caa,cab,cac,caa,caa,cab,cac,cba,cba,cbb,cbc,cca,cca,ccb,ccc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,aaaa,aaaa,aaab,aaac,aaaa,aaaa,aaab,aaac,aaba,aaba,aabb,aabc,aaca,aaca,aacb,aacc,abaa,abaa,abab,abac,abaa,abaa,abab,abac,abba,abba,abbb,abbc,abca,abca,abcb,abcc,acaa,acaa,acab,acac,acaa,acaa,acab,acac,acba,acba,acbb,acbc,acca,acca,accb,accc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,baaa,baaa,baab,baac,baaa,baaa,baab,baac,baba,baba,babb,babc,baca,baca,bacb,bacc,bbaa,bbaa,bbab,bbac,bbaa,bbaa,bbab,bbac,bbba,bbba,bbbb,bbbc,bbca,bbca,bbcb,bbcc,bcaa,bcaa,bcab,bcac,bcaa,bcaa,bcab,bcac,bcba,bcba,bcbb,bcbc,bcca,bcca,bccb,bccc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,caaa,caaa,caab,caac,caaa,caaa,caab,caac,caba,caba,cabb,cabc,caca,caca,cacb,cacc,cbaa,cbaa,cbab,cbac,cbaa,cbaa,cbab,cbac,cbba,cbba,cbbb,cbbc,cbca,cbca,cbcb,cbcc,ccaa,ccaa,ccab,ccac,ccaa,ccaa,ccab,ccac,ccba,ccba,ccbb,ccbc,ccca,ccca,cccb,cccc]
Q - de-duplicate [a,b,c,aa,ab,ac,ba,bb,bc,ca,cb,cc,aaa,aab,aac,aba,abb,abc,aca,acb,acc,baa,bab,bac,bba,bbb,bbc,bca,bcb,bcc,caa,cab,cac,cba,cbb,cbc,cca,ccb,ccc,aaaa,aaab,aaac,aaba,aabb,aabc,aaca,aacb,aacc,abaa,abab,abac,abba,abbb,abbc,abca,abcb,abcc,acaa,acab,acac,acba,acbb,acbc,acca,accb,accc,baaa,baab,baac,baba,babb,babc,baca,bacb,bacc,bbaa,bbab,bbac,bbba,bbbb,bbbc,bbca,bbcb,bbcc,bcaa,bcab,bcac,bcba,bcbb,bcbc,bcca,bccb,bccc,caaa,caab,caac,caba,cabb,cabc,caca,cacb,cacc,cbaa,cbab,cbac,cbba,cbbb,cbbc,cbca,cbcb,cbcc,ccaa,ccab,ccac,ccba,ccbb,ccbc,ccca,cccb,cccc]
X - uniform random choice
edited Apr 5 at 22:28
answered Apr 5 at 19:47
Jonathan AllanJonathan Allan
54.2k537174
54.2k537174
add a comment |
add a comment |
$begingroup$
05AB1E, 6 bytes
ā€ã˜ÙΩ
Try it online!
Explanation
ā # push [1 ... len(input)]
ۋ # apply repeated cartesian product on each and input
˜ # flatten
Ù # remove duplicates
Ω # pick random string
$endgroup$
add a comment |
$begingroup$
05AB1E, 6 bytes
ā€ã˜ÙΩ
Try it online!
Explanation
ā # push [1 ... len(input)]
ۋ # apply repeated cartesian product on each and input
˜ # flatten
Ù # remove duplicates
Ω # pick random string
$endgroup$
add a comment |
$begingroup$
05AB1E, 6 bytes
ā€ã˜ÙΩ
Try it online!
Explanation
ā # push [1 ... len(input)]
ۋ # apply repeated cartesian product on each and input
˜ # flatten
Ù # remove duplicates
Ω # pick random string
$endgroup$
05AB1E, 6 bytes
ā€ã˜ÙΩ
Try it online!
Explanation
ā # push [1 ... len(input)]
ۋ # apply repeated cartesian product on each and input
˜ # flatten
Ù # remove duplicates
Ω # pick random string
edited Apr 6 at 8:06
answered Apr 5 at 20:43
EmignaEmigna
47.9k434146
47.9k434146
add a comment |
add a comment |
$begingroup$
Perl 6, 35 33 28 bytes
-2 bytes thanks to nwellnhof
{[~] roll .rand+1,$_}o*.comb
Try it online!
This challenge keeps changing a lot.
Explanation:
{ }o*.comb # Anonymous code block turning input into chars
$_ # From the input
.rand+1, # Take a random number from 1 to length of list
roll # Of characters
[~] # And join them into a string
$endgroup$
$begingroup$
33 bytes
$endgroup$
– nwellnhof
Apr 7 at 9:05
add a comment |
$begingroup$
Perl 6, 35 33 28 bytes
-2 bytes thanks to nwellnhof
{[~] roll .rand+1,$_}o*.comb
Try it online!
This challenge keeps changing a lot.
Explanation:
{ }o*.comb # Anonymous code block turning input into chars
$_ # From the input
.rand+1, # Take a random number from 1 to length of list
roll # Of characters
[~] # And join them into a string
$endgroup$
$begingroup$
33 bytes
$endgroup$
– nwellnhof
Apr 7 at 9:05
add a comment |
$begingroup$
Perl 6, 35 33 28 bytes
-2 bytes thanks to nwellnhof
{[~] roll .rand+1,$_}o*.comb
Try it online!
This challenge keeps changing a lot.
Explanation:
{ }o*.comb # Anonymous code block turning input into chars
$_ # From the input
.rand+1, # Take a random number from 1 to length of list
roll # Of characters
[~] # And join them into a string
$endgroup$
Perl 6, 35 33 28 bytes
-2 bytes thanks to nwellnhof
{[~] roll .rand+1,$_}o*.comb
Try it online!
This challenge keeps changing a lot.
Explanation:
{ }o*.comb # Anonymous code block turning input into chars
$_ # From the input
.rand+1, # Take a random number from 1 to length of list
roll # Of characters
[~] # And join them into a string
edited Apr 10 at 0:30
answered Apr 6 at 2:31
Jo KingJo King
26.7k365132
26.7k365132
$begingroup$
33 bytes
$endgroup$
– nwellnhof
Apr 7 at 9:05
add a comment |
$begingroup$
33 bytes
$endgroup$
– nwellnhof
Apr 7 at 9:05
$begingroup$
33 bytes
$endgroup$
– nwellnhof
Apr 7 at 9:05
$begingroup$
33 bytes
$endgroup$
– nwellnhof
Apr 7 at 9:05
add a comment |
$begingroup$
Brachylog, 8 bytes
⊇ᶠbṛ;?ṛw
Try it online!
Prints output directly instead of constraining the output variable, because for some reason this actually fails without the w
at the end. Originally this used ≠
and the ∈&
backtracking hack I used to implement bogosort, but I realized that it would be far saner to just use b
instead.
w Print
ṛ a random one of
? the input
; or
ṛ a random element of
ᶠ every
⊇ sublist of
the input
b except the first one (which would be the input).
$endgroup$
add a comment |
$begingroup$
Brachylog, 8 bytes
⊇ᶠbṛ;?ṛw
Try it online!
Prints output directly instead of constraining the output variable, because for some reason this actually fails without the w
at the end. Originally this used ≠
and the ∈&
backtracking hack I used to implement bogosort, but I realized that it would be far saner to just use b
instead.
w Print
ṛ a random one of
? the input
; or
ṛ a random element of
ᶠ every
⊇ sublist of
the input
b except the first one (which would be the input).
$endgroup$
add a comment |
$begingroup$
Brachylog, 8 bytes
⊇ᶠbṛ;?ṛw
Try it online!
Prints output directly instead of constraining the output variable, because for some reason this actually fails without the w
at the end. Originally this used ≠
and the ∈&
backtracking hack I used to implement bogosort, but I realized that it would be far saner to just use b
instead.
w Print
ṛ a random one of
? the input
; or
ṛ a random element of
ᶠ every
⊇ sublist of
the input
b except the first one (which would be the input).
$endgroup$
Brachylog, 8 bytes
⊇ᶠbṛ;?ṛw
Try it online!
Prints output directly instead of constraining the output variable, because for some reason this actually fails without the w
at the end. Originally this used ≠
and the ∈&
backtracking hack I used to implement bogosort, but I realized that it would be far saner to just use b
instead.
w Print
ṛ a random one of
? the input
; or
ṛ a random element of
ᶠ every
⊇ sublist of
the input
b except the first one (which would be the input).
answered Apr 5 at 20:29
Unrelated StringUnrelated String
1,651312
1,651312
add a comment |
add a comment |
$begingroup$
Python 2, 111 121 bytes
lambda s:choice(g(list(set(s)),len(s)))
from random import *
g=lambda s,n:s+(n>1and[c+k for k in g(s,n-1)for c in s]or)
Try it online!
Takes any iterable (list, set, string, etc.) of either distinct characters or non-distinct characters as input. Outputs a non-empty string with the required distribution.
g
is a recursive function to generate a list of compliant strings.
$endgroup$
add a comment |
$begingroup$
Python 2, 111 121 bytes
lambda s:choice(g(list(set(s)),len(s)))
from random import *
g=lambda s,n:s+(n>1and[c+k for k in g(s,n-1)for c in s]or)
Try it online!
Takes any iterable (list, set, string, etc.) of either distinct characters or non-distinct characters as input. Outputs a non-empty string with the required distribution.
g
is a recursive function to generate a list of compliant strings.
$endgroup$
add a comment |
$begingroup$
Python 2, 111 121 bytes
lambda s:choice(g(list(set(s)),len(s)))
from random import *
g=lambda s,n:s+(n>1and[c+k for k in g(s,n-1)for c in s]or)
Try it online!
Takes any iterable (list, set, string, etc.) of either distinct characters or non-distinct characters as input. Outputs a non-empty string with the required distribution.
g
is a recursive function to generate a list of compliant strings.
$endgroup$
Python 2, 111 121 bytes
lambda s:choice(g(list(set(s)),len(s)))
from random import *
g=lambda s,n:s+(n>1and[c+k for k in g(s,n-1)for c in s]or)
Try it online!
Takes any iterable (list, set, string, etc.) of either distinct characters or non-distinct characters as input. Outputs a non-empty string with the required distribution.
g
is a recursive function to generate a list of compliant strings.
edited Apr 6 at 6:49
answered Apr 6 at 4:04
Chas BrownChas Brown
5,2291523
5,2291523
add a comment |
add a comment |
$begingroup$
R, 72 64 bytes
-8 bytes thanks to Giuseppe.
S=sample;S(t<-unique(s<-scan(,"")),S(n<-seq(s),p=length(t)^n),T)
Try it online!
Input and output are vectors of characters. All possible outputs have equal probability.
Explanation (ungolfed version):
s<-scan(,"") # takes input as vector of characters
n<-length(s)
t<-unique(s)
sample(t, # sample uniformly at random from the list of unique characters
sample(n,p=length(t)^(1:n)), # with length k in 1..n chosen randomly such that P[k] is proportional to length(t)^k
T) # the sampling is with replacement
$endgroup$
1
$begingroup$
64 bytes
$endgroup$
– Giuseppe
Apr 10 at 1:36
$begingroup$
@Giuseppe Thanks!
$endgroup$
– Robin Ryder
Apr 10 at 5:52
add a comment |
$begingroup$
R, 72 64 bytes
-8 bytes thanks to Giuseppe.
S=sample;S(t<-unique(s<-scan(,"")),S(n<-seq(s),p=length(t)^n),T)
Try it online!
Input and output are vectors of characters. All possible outputs have equal probability.
Explanation (ungolfed version):
s<-scan(,"") # takes input as vector of characters
n<-length(s)
t<-unique(s)
sample(t, # sample uniformly at random from the list of unique characters
sample(n,p=length(t)^(1:n)), # with length k in 1..n chosen randomly such that P[k] is proportional to length(t)^k
T) # the sampling is with replacement
$endgroup$
1
$begingroup$
64 bytes
$endgroup$
– Giuseppe
Apr 10 at 1:36
$begingroup$
@Giuseppe Thanks!
$endgroup$
– Robin Ryder
Apr 10 at 5:52
add a comment |
$begingroup$
R, 72 64 bytes
-8 bytes thanks to Giuseppe.
S=sample;S(t<-unique(s<-scan(,"")),S(n<-seq(s),p=length(t)^n),T)
Try it online!
Input and output are vectors of characters. All possible outputs have equal probability.
Explanation (ungolfed version):
s<-scan(,"") # takes input as vector of characters
n<-length(s)
t<-unique(s)
sample(t, # sample uniformly at random from the list of unique characters
sample(n,p=length(t)^(1:n)), # with length k in 1..n chosen randomly such that P[k] is proportional to length(t)^k
T) # the sampling is with replacement
$endgroup$
R, 72 64 bytes
-8 bytes thanks to Giuseppe.
S=sample;S(t<-unique(s<-scan(,"")),S(n<-seq(s),p=length(t)^n),T)
Try it online!
Input and output are vectors of characters. All possible outputs have equal probability.
Explanation (ungolfed version):
s<-scan(,"") # takes input as vector of characters
n<-length(s)
t<-unique(s)
sample(t, # sample uniformly at random from the list of unique characters
sample(n,p=length(t)^(1:n)), # with length k in 1..n chosen randomly such that P[k] is proportional to length(t)^k
T) # the sampling is with replacement
edited Apr 10 at 5:51
answered Apr 6 at 7:28
Robin RyderRobin Ryder
6719
6719
1
$begingroup$
64 bytes
$endgroup$
– Giuseppe
Apr 10 at 1:36
$begingroup$
@Giuseppe Thanks!
$endgroup$
– Robin Ryder
Apr 10 at 5:52
add a comment |
1
$begingroup$
64 bytes
$endgroup$
– Giuseppe
Apr 10 at 1:36
$begingroup$
@Giuseppe Thanks!
$endgroup$
– Robin Ryder
Apr 10 at 5:52
1
1
$begingroup$
64 bytes
$endgroup$
– Giuseppe
Apr 10 at 1:36
$begingroup$
64 bytes
$endgroup$
– Giuseppe
Apr 10 at 1:36
$begingroup$
@Giuseppe Thanks!
$endgroup$
– Robin Ryder
Apr 10 at 5:52
$begingroup$
@Giuseppe Thanks!
$endgroup$
– Robin Ryder
Apr 10 at 5:52
add a comment |
$begingroup$
Wolfram Language (Mathematica), 41 bytes
""<>#&@*RandomChoice@*Subsets@*Characters
Try it online!
$endgroup$
$begingroup$
Wha is this part?""<>#&@
$endgroup$
– Jonah
Apr 6 at 21:30
1
$begingroup$
@Jonah<>
isStringJoin
;""<>#&
is an anonymous function that joins""
with its argument (and is much shorter thanStringJoin
).@*
composes functions.
$endgroup$
– attinat
Apr 7 at 18:19
add a comment |
$begingroup$
Wolfram Language (Mathematica), 41 bytes
""<>#&@*RandomChoice@*Subsets@*Characters
Try it online!
$endgroup$
$begingroup$
Wha is this part?""<>#&@
$endgroup$
– Jonah
Apr 6 at 21:30
1
$begingroup$
@Jonah<>
isStringJoin
;""<>#&
is an anonymous function that joins""
with its argument (and is much shorter thanStringJoin
).@*
composes functions.
$endgroup$
– attinat
Apr 7 at 18:19
add a comment |
$begingroup$
Wolfram Language (Mathematica), 41 bytes
""<>#&@*RandomChoice@*Subsets@*Characters
Try it online!
$endgroup$
Wolfram Language (Mathematica), 41 bytes
""<>#&@*RandomChoice@*Subsets@*Characters
Try it online!
answered Apr 6 at 10:25
attinatattinat
4897
4897
$begingroup$
Wha is this part?""<>#&@
$endgroup$
– Jonah
Apr 6 at 21:30
1
$begingroup$
@Jonah<>
isStringJoin
;""<>#&
is an anonymous function that joins""
with its argument (and is much shorter thanStringJoin
).@*
composes functions.
$endgroup$
– attinat
Apr 7 at 18:19
add a comment |
$begingroup$
Wha is this part?""<>#&@
$endgroup$
– Jonah
Apr 6 at 21:30
1
$begingroup$
@Jonah<>
isStringJoin
;""<>#&
is an anonymous function that joins""
with its argument (and is much shorter thanStringJoin
).@*
composes functions.
$endgroup$
– attinat
Apr 7 at 18:19
$begingroup$
Wha is this part?
""<>#&@
$endgroup$
– Jonah
Apr 6 at 21:30
$begingroup$
Wha is this part?
""<>#&@
$endgroup$
– Jonah
Apr 6 at 21:30
1
1
$begingroup$
@Jonah
<>
is StringJoin
; ""<>#&
is an anonymous function that joins ""
with its argument (and is much shorter than StringJoin
). @*
composes functions.$endgroup$
– attinat
Apr 7 at 18:19
$begingroup$
@Jonah
<>
is StringJoin
; ""<>#&
is an anonymous function that joins ""
with its argument (and is much shorter than StringJoin
). @*
composes functions.$endgroup$
– attinat
Apr 7 at 18:19
add a comment |
$begingroup$
Java 131 bytes
void k(String a){a.chars().forEach(v->{int z=(int)(Math.random()*(a.length()+1));if(z<a.length())System.out.print(a.charAt(z));});}
$endgroup$
$begingroup$
Nice approach! Since you're using a Java 8+ stream, why not changevoid k(String a){a.chars()..;}
toa->a.chars()..
as well? Also,int z=(int)(Math.random()*(a.length()+1));if(z<a.length())
can beint l=a.length(),z=l+1;z*=Math.random();if(z<l)
to save some more bytes. In total it then becomes 104 bytes
$endgroup$
– Kevin Cruijssen
2 days ago
add a comment |
$begingroup$
Java 131 bytes
void k(String a){a.chars().forEach(v->{int z=(int)(Math.random()*(a.length()+1));if(z<a.length())System.out.print(a.charAt(z));});}
$endgroup$
$begingroup$
Nice approach! Since you're using a Java 8+ stream, why not changevoid k(String a){a.chars()..;}
toa->a.chars()..
as well? Also,int z=(int)(Math.random()*(a.length()+1));if(z<a.length())
can beint l=a.length(),z=l+1;z*=Math.random();if(z<l)
to save some more bytes. In total it then becomes 104 bytes
$endgroup$
– Kevin Cruijssen
2 days ago
add a comment |
$begingroup$
Java 131 bytes
void k(String a){a.chars().forEach(v->{int z=(int)(Math.random()*(a.length()+1));if(z<a.length())System.out.print(a.charAt(z));});}
$endgroup$
Java 131 bytes
void k(String a){a.chars().forEach(v->{int z=(int)(Math.random()*(a.length()+1));if(z<a.length())System.out.print(a.charAt(z));});}
answered Apr 8 at 15:05
Ilya GazmanIlya Gazman
489313
489313
$begingroup$
Nice approach! Since you're using a Java 8+ stream, why not changevoid k(String a){a.chars()..;}
toa->a.chars()..
as well? Also,int z=(int)(Math.random()*(a.length()+1));if(z<a.length())
can beint l=a.length(),z=l+1;z*=Math.random();if(z<l)
to save some more bytes. In total it then becomes 104 bytes
$endgroup$
– Kevin Cruijssen
2 days ago
add a comment |
$begingroup$
Nice approach! Since you're using a Java 8+ stream, why not changevoid k(String a){a.chars()..;}
toa->a.chars()..
as well? Also,int z=(int)(Math.random()*(a.length()+1));if(z<a.length())
can beint l=a.length(),z=l+1;z*=Math.random();if(z<l)
to save some more bytes. In total it then becomes 104 bytes
$endgroup$
– Kevin Cruijssen
2 days ago
$begingroup$
Nice approach! Since you're using a Java 8+ stream, why not change
void k(String a){a.chars()..;}
to a->a.chars()..
as well? Also, int z=(int)(Math.random()*(a.length()+1));if(z<a.length())
can be int l=a.length(),z=l+1;z*=Math.random();if(z<l)
to save some more bytes. In total it then becomes 104 bytes$endgroup$
– Kevin Cruijssen
2 days ago
$begingroup$
Nice approach! Since you're using a Java 8+ stream, why not change
void k(String a){a.chars()..;}
to a->a.chars()..
as well? Also, int z=(int)(Math.random()*(a.length()+1));if(z<a.length())
can be int l=a.length(),z=l+1;z*=Math.random();if(z<l)
to save some more bytes. In total it then becomes 104 bytes$endgroup$
– Kevin Cruijssen
2 days ago
add a comment |
$begingroup$
Pyth - 7 bytes
Os^LQSl
O Random choice
s Collapse list
^L Map cartesian power
Q Input
S 1-indexed range
l Length
(Q implicit) Input
Try it online.
Try it online without random pick to see all possible options.
$endgroup$
add a comment |
$begingroup$
Pyth - 7 bytes
Os^LQSl
O Random choice
s Collapse list
^L Map cartesian power
Q Input
S 1-indexed range
l Length
(Q implicit) Input
Try it online.
Try it online without random pick to see all possible options.
$endgroup$
add a comment |
$begingroup$
Pyth - 7 bytes
Os^LQSl
O Random choice
s Collapse list
^L Map cartesian power
Q Input
S 1-indexed range
l Length
(Q implicit) Input
Try it online.
Try it online without random pick to see all possible options.
$endgroup$
Pyth - 7 bytes
Os^LQSl
O Random choice
s Collapse list
^L Map cartesian power
Q Input
S 1-indexed range
l Length
(Q implicit) Input
Try it online.
Try it online without random pick to see all possible options.
answered 2 days ago
MaltysenMaltysen
21.4k445116
21.4k445116
add a comment |
add a comment |
$begingroup$
C (gcc), 68 65 bytes
f(s,i)char*s;{for(;i<strlen(s);i++)rand()&1?putc(s[i],stdout):0;}
Try it online!
$endgroup$
$begingroup$
I don't think this is correct anymore
$endgroup$
– Jo King
Apr 6 at 3:02
1
$begingroup$
If the op changed the rules that's a pity.
$endgroup$
– Natural Number Guy
Apr 6 at 11:45
add a comment |
$begingroup$
C (gcc), 68 65 bytes
f(s,i)char*s;{for(;i<strlen(s);i++)rand()&1?putc(s[i],stdout):0;}
Try it online!
$endgroup$
$begingroup$
I don't think this is correct anymore
$endgroup$
– Jo King
Apr 6 at 3:02
1
$begingroup$
If the op changed the rules that's a pity.
$endgroup$
– Natural Number Guy
Apr 6 at 11:45
add a comment |
$begingroup$
C (gcc), 68 65 bytes
f(s,i)char*s;{for(;i<strlen(s);i++)rand()&1?putc(s[i],stdout):0;}
Try it online!
$endgroup$
C (gcc), 68 65 bytes
f(s,i)char*s;{for(;i<strlen(s);i++)rand()&1?putc(s[i],stdout):0;}
Try it online!
edited Apr 5 at 20:01
answered Apr 5 at 19:49
Natural Number GuyNatural Number Guy
1516
1516
$begingroup$
I don't think this is correct anymore
$endgroup$
– Jo King
Apr 6 at 3:02
1
$begingroup$
If the op changed the rules that's a pity.
$endgroup$
– Natural Number Guy
Apr 6 at 11:45
add a comment |
$begingroup$
I don't think this is correct anymore
$endgroup$
– Jo King
Apr 6 at 3:02
1
$begingroup$
If the op changed the rules that's a pity.
$endgroup$
– Natural Number Guy
Apr 6 at 11:45
$begingroup$
I don't think this is correct anymore
$endgroup$
– Jo King
Apr 6 at 3:02
$begingroup$
I don't think this is correct anymore
$endgroup$
– Jo King
Apr 6 at 3:02
1
1
$begingroup$
If the op changed the rules that's a pity.
$endgroup$
– Natural Number Guy
Apr 6 at 11:45
$begingroup$
If the op changed the rules that's a pity.
$endgroup$
– Natural Number Guy
Apr 6 at 11:45
add a comment |
$begingroup$
Charcoal, 34 bytes
≔Φθ⁼κ⌕θιη≔⊕‽ΣEθXLη⊕κζW櫧ηζ≔÷⊖ζLηζ
Try it online! Link is to verbose version of code. Explanation:
≔Φθ⁼κ⌕θιη
Extract the unique characters of the input. Let's call the number of unique characters n
.
≔⊕‽ΣEθXLη⊕κζ
Calculate the number of combinations for each possible length and take the sum. Then, pick a random number between 1 and this number (inclusive). This ensures that all combinations are equally likely (within the accuracy of the random number generator).
W櫧ηζ≔÷⊖ζLηζ
Convert the number into bijective base n
, using the unique characters as the digits.
$endgroup$
add a comment |
$begingroup$
Charcoal, 34 bytes
≔Φθ⁼κ⌕θιη≔⊕‽ΣEθXLη⊕κζW櫧ηζ≔÷⊖ζLηζ
Try it online! Link is to verbose version of code. Explanation:
≔Φθ⁼κ⌕θιη
Extract the unique characters of the input. Let's call the number of unique characters n
.
≔⊕‽ΣEθXLη⊕κζ
Calculate the number of combinations for each possible length and take the sum. Then, pick a random number between 1 and this number (inclusive). This ensures that all combinations are equally likely (within the accuracy of the random number generator).
W櫧ηζ≔÷⊖ζLηζ
Convert the number into bijective base n
, using the unique characters as the digits.
$endgroup$
add a comment |
$begingroup$
Charcoal, 34 bytes
≔Φθ⁼κ⌕θιη≔⊕‽ΣEθXLη⊕κζW櫧ηζ≔÷⊖ζLηζ
Try it online! Link is to verbose version of code. Explanation:
≔Φθ⁼κ⌕θιη
Extract the unique characters of the input. Let's call the number of unique characters n
.
≔⊕‽ΣEθXLη⊕κζ
Calculate the number of combinations for each possible length and take the sum. Then, pick a random number between 1 and this number (inclusive). This ensures that all combinations are equally likely (within the accuracy of the random number generator).
W櫧ηζ≔÷⊖ζLηζ
Convert the number into bijective base n
, using the unique characters as the digits.
$endgroup$
Charcoal, 34 bytes
≔Φθ⁼κ⌕θιη≔⊕‽ΣEθXLη⊕κζW櫧ηζ≔÷⊖ζLηζ
Try it online! Link is to verbose version of code. Explanation:
≔Φθ⁼κ⌕θιη
Extract the unique characters of the input. Let's call the number of unique characters n
.
≔⊕‽ΣEθXLη⊕κζ
Calculate the number of combinations for each possible length and take the sum. Then, pick a random number between 1 and this number (inclusive). This ensures that all combinations are equally likely (within the accuracy of the random number generator).
W櫧ηζ≔÷⊖ζLηζ
Convert the number into bijective base n
, using the unique characters as the digits.
answered Apr 6 at 9:48
NeilNeil
82.8k745179
82.8k745179
add a comment |
add a comment |
$begingroup$
MATLAB / Octave, 110 bytes
Choose a random permutation of a subset of the input letters (with repetitions), whose random length is based on the probability of generating a word with that length.
@(a)a(randi(numel(a),[find(cumsum(numel(a).^[1:numel(a)])>=randi(sum(numel(a).^[1:numel(a)])),1,'first'),1]));
Try it online!
$endgroup$
add a comment |
$begingroup$
MATLAB / Octave, 110 bytes
Choose a random permutation of a subset of the input letters (with repetitions), whose random length is based on the probability of generating a word with that length.
@(a)a(randi(numel(a),[find(cumsum(numel(a).^[1:numel(a)])>=randi(sum(numel(a).^[1:numel(a)])),1,'first'),1]));
Try it online!
$endgroup$
add a comment |
$begingroup$
MATLAB / Octave, 110 bytes
Choose a random permutation of a subset of the input letters (with repetitions), whose random length is based on the probability of generating a word with that length.
@(a)a(randi(numel(a),[find(cumsum(numel(a).^[1:numel(a)])>=randi(sum(numel(a).^[1:numel(a)])),1,'first'),1]));
Try it online!
$endgroup$
MATLAB / Octave, 110 bytes
Choose a random permutation of a subset of the input letters (with repetitions), whose random length is based on the probability of generating a word with that length.
@(a)a(randi(numel(a),[find(cumsum(numel(a).^[1:numel(a)])>=randi(sum(numel(a).^[1:numel(a)])),1,'first'),1]));
Try it online!
answered Apr 6 at 12:27
PieCotPieCot
97959
97959
add a comment |
add a comment |
$begingroup$
T-SQL, 222 bytes
This creates all combinations of each unique character with recursive sql, then picks a random row from the distinct combinations.
DECLARE @ varchar(max)='T-SQL';
WITH C as(SELECT DISTINCT substring(@,number+1,1)x
FROM spt_values
WHERE'P'=type and len(@)>number),D
as(SELECT x y
FROM c UNION ALL
SELECT y+x
FROM C JOIN D
ON len(y)<len(@))SELECT top 1*FROM D
GROUP BY y
ORDER BY newid()
Note the online version will always give the same result unlike MS-SQL Studio Management. This is because newid() always returns the same value in the online testing. This should work in Studio Management.
Try it online ungolfed version
$endgroup$
add a comment |
$begingroup$
T-SQL, 222 bytes
This creates all combinations of each unique character with recursive sql, then picks a random row from the distinct combinations.
DECLARE @ varchar(max)='T-SQL';
WITH C as(SELECT DISTINCT substring(@,number+1,1)x
FROM spt_values
WHERE'P'=type and len(@)>number),D
as(SELECT x y
FROM c UNION ALL
SELECT y+x
FROM C JOIN D
ON len(y)<len(@))SELECT top 1*FROM D
GROUP BY y
ORDER BY newid()
Note the online version will always give the same result unlike MS-SQL Studio Management. This is because newid() always returns the same value in the online testing. This should work in Studio Management.
Try it online ungolfed version
$endgroup$
add a comment |
$begingroup$
T-SQL, 222 bytes
This creates all combinations of each unique character with recursive sql, then picks a random row from the distinct combinations.
DECLARE @ varchar(max)='T-SQL';
WITH C as(SELECT DISTINCT substring(@,number+1,1)x
FROM spt_values
WHERE'P'=type and len(@)>number),D
as(SELECT x y
FROM c UNION ALL
SELECT y+x
FROM C JOIN D
ON len(y)<len(@))SELECT top 1*FROM D
GROUP BY y
ORDER BY newid()
Note the online version will always give the same result unlike MS-SQL Studio Management. This is because newid() always returns the same value in the online testing. This should work in Studio Management.
Try it online ungolfed version
$endgroup$
T-SQL, 222 bytes
This creates all combinations of each unique character with recursive sql, then picks a random row from the distinct combinations.
DECLARE @ varchar(max)='T-SQL';
WITH C as(SELECT DISTINCT substring(@,number+1,1)x
FROM spt_values
WHERE'P'=type and len(@)>number),D
as(SELECT x y
FROM c UNION ALL
SELECT y+x
FROM C JOIN D
ON len(y)<len(@))SELECT top 1*FROM D
GROUP BY y
ORDER BY newid()
Note the online version will always give the same result unlike MS-SQL Studio Management. This is because newid() always returns the same value in the online testing. This should work in Studio Management.
Try it online ungolfed version
edited Apr 6 at 18:48
answered Apr 6 at 9:16
t-clausen.dkt-clausen.dk
2,084314
2,084314
add a comment |
add a comment |
$begingroup$
Python 2, 124 bytes
lambda s:g(list(set(s)),len(s))
from random import*
g=lambda s,n:choice(s)+(random()*~-len(s)**(n)>~-len(s)and g(s,n-1)or'')
Try it online!
A different approach: instead of first constructing a list of all compliant strings and choosing one at random, this approach randomly decides to continue extending the string or stopping.
This again has to deal with the exacting input requirements, at a cost of 31 bytes; but the function of interest g
takes a list s
of characters to be used, and an integer n
which is the maximum length of the returned string.
As an example, consider the set of characters ['a','b'] and suppose we want to generate random strings of length 1, 2, or 3.
Then if we randomly choose 'a' as the starting character, the possible strings which could be returned are:
a
aa
aaa
aab
ab
aba
abb
shown above in 'tree' form, which is a total of 1 + 2 + 2*2 = 2^0 + 2^1 + 2^2 = 7
strings. So if we generate a string a
; then 1/7
of the time we should stop and return 'a'
, and 6/7
of the time we should add further to the string - then the distribution will be uniform in the way desired.
More generally, if n
is maximum length of the string and x
is the number of characters in the set, then the number of strings starting with some given character is going to be:
$$h(n)=sum_{i=0}^{n-1} x^i$$
$$h(n)=1+x+x^2+x^3...+x^{n-1}$$
For x>1
(guaranteed by OP's rule 'at least two distinct...'), we have:
$$h(n)(x-1)=(1+x+x^2+x^3...+x^{n-1})(x-1)=x^n-1$$
$$h(n)=frac{x^n-1}{x-1}$$
So to decide whether we should continue extending our string, let p
be a random number in [0,1)
; then we should recurse only if any of these equivalent statements are true:
$$p>frac{1}{h(n)}$$
$$p>frac{x-1}{x^n-1}$$
$$p(x^n-1)>x-1$$
of which g
is the golfed implementation.
$endgroup$
add a comment |
$begingroup$
Python 2, 124 bytes
lambda s:g(list(set(s)),len(s))
from random import*
g=lambda s,n:choice(s)+(random()*~-len(s)**(n)>~-len(s)and g(s,n-1)or'')
Try it online!
A different approach: instead of first constructing a list of all compliant strings and choosing one at random, this approach randomly decides to continue extending the string or stopping.
This again has to deal with the exacting input requirements, at a cost of 31 bytes; but the function of interest g
takes a list s
of characters to be used, and an integer n
which is the maximum length of the returned string.
As an example, consider the set of characters ['a','b'] and suppose we want to generate random strings of length 1, 2, or 3.
Then if we randomly choose 'a' as the starting character, the possible strings which could be returned are:
a
aa
aaa
aab
ab
aba
abb
shown above in 'tree' form, which is a total of 1 + 2 + 2*2 = 2^0 + 2^1 + 2^2 = 7
strings. So if we generate a string a
; then 1/7
of the time we should stop and return 'a'
, and 6/7
of the time we should add further to the string - then the distribution will be uniform in the way desired.
More generally, if n
is maximum length of the string and x
is the number of characters in the set, then the number of strings starting with some given character is going to be:
$$h(n)=sum_{i=0}^{n-1} x^i$$
$$h(n)=1+x+x^2+x^3...+x^{n-1}$$
For x>1
(guaranteed by OP's rule 'at least two distinct...'), we have:
$$h(n)(x-1)=(1+x+x^2+x^3...+x^{n-1})(x-1)=x^n-1$$
$$h(n)=frac{x^n-1}{x-1}$$
So to decide whether we should continue extending our string, let p
be a random number in [0,1)
; then we should recurse only if any of these equivalent statements are true:
$$p>frac{1}{h(n)}$$
$$p>frac{x-1}{x^n-1}$$
$$p(x^n-1)>x-1$$
of which g
is the golfed implementation.
$endgroup$
add a comment |
$begingroup$
Python 2, 124 bytes
lambda s:g(list(set(s)),len(s))
from random import*
g=lambda s,n:choice(s)+(random()*~-len(s)**(n)>~-len(s)and g(s,n-1)or'')
Try it online!
A different approach: instead of first constructing a list of all compliant strings and choosing one at random, this approach randomly decides to continue extending the string or stopping.
This again has to deal with the exacting input requirements, at a cost of 31 bytes; but the function of interest g
takes a list s
of characters to be used, and an integer n
which is the maximum length of the returned string.
As an example, consider the set of characters ['a','b'] and suppose we want to generate random strings of length 1, 2, or 3.
Then if we randomly choose 'a' as the starting character, the possible strings which could be returned are:
a
aa
aaa
aab
ab
aba
abb
shown above in 'tree' form, which is a total of 1 + 2 + 2*2 = 2^0 + 2^1 + 2^2 = 7
strings. So if we generate a string a
; then 1/7
of the time we should stop and return 'a'
, and 6/7
of the time we should add further to the string - then the distribution will be uniform in the way desired.
More generally, if n
is maximum length of the string and x
is the number of characters in the set, then the number of strings starting with some given character is going to be:
$$h(n)=sum_{i=0}^{n-1} x^i$$
$$h(n)=1+x+x^2+x^3...+x^{n-1}$$
For x>1
(guaranteed by OP's rule 'at least two distinct...'), we have:
$$h(n)(x-1)=(1+x+x^2+x^3...+x^{n-1})(x-1)=x^n-1$$
$$h(n)=frac{x^n-1}{x-1}$$
So to decide whether we should continue extending our string, let p
be a random number in [0,1)
; then we should recurse only if any of these equivalent statements are true:
$$p>frac{1}{h(n)}$$
$$p>frac{x-1}{x^n-1}$$
$$p(x^n-1)>x-1$$
of which g
is the golfed implementation.
$endgroup$
Python 2, 124 bytes
lambda s:g(list(set(s)),len(s))
from random import*
g=lambda s,n:choice(s)+(random()*~-len(s)**(n)>~-len(s)and g(s,n-1)or'')
Try it online!
A different approach: instead of first constructing a list of all compliant strings and choosing one at random, this approach randomly decides to continue extending the string or stopping.
This again has to deal with the exacting input requirements, at a cost of 31 bytes; but the function of interest g
takes a list s
of characters to be used, and an integer n
which is the maximum length of the returned string.
As an example, consider the set of characters ['a','b'] and suppose we want to generate random strings of length 1, 2, or 3.
Then if we randomly choose 'a' as the starting character, the possible strings which could be returned are:
a
aa
aaa
aab
ab
aba
abb
shown above in 'tree' form, which is a total of 1 + 2 + 2*2 = 2^0 + 2^1 + 2^2 = 7
strings. So if we generate a string a
; then 1/7
of the time we should stop and return 'a'
, and 6/7
of the time we should add further to the string - then the distribution will be uniform in the way desired.
More generally, if n
is maximum length of the string and x
is the number of characters in the set, then the number of strings starting with some given character is going to be:
$$h(n)=sum_{i=0}^{n-1} x^i$$
$$h(n)=1+x+x^2+x^3...+x^{n-1}$$
For x>1
(guaranteed by OP's rule 'at least two distinct...'), we have:
$$h(n)(x-1)=(1+x+x^2+x^3...+x^{n-1})(x-1)=x^n-1$$
$$h(n)=frac{x^n-1}{x-1}$$
So to decide whether we should continue extending our string, let p
be a random number in [0,1)
; then we should recurse only if any of these equivalent statements are true:
$$p>frac{1}{h(n)}$$
$$p>frac{x-1}{x^n-1}$$
$$p(x^n-1)>x-1$$
of which g
is the golfed implementation.
answered Apr 6 at 20:36
Chas BrownChas Brown
5,2291523
5,2291523
add a comment |
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 126 bytes
a=>{var s=new Random();int d=a.Length;return Enumerable.Range(1,s.Next(1,d)).Select(x=>a.Distinct().ToList()[s.Next(0,d-1)]);}
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 126 bytes
a=>{var s=new Random();int d=a.Length;return Enumerable.Range(1,s.Next(1,d)).Select(x=>a.Distinct().ToList()[s.Next(0,d-1)]);}
Try it online!
$endgroup$
add a comment |
$begingroup$
C# (Visual C# Interactive Compiler), 126 bytes
a=>{var s=new Random();int d=a.Length;return Enumerable.Range(1,s.Next(1,d)).Select(x=>a.Distinct().ToList()[s.Next(0,d-1)]);}
Try it online!
$endgroup$
C# (Visual C# Interactive Compiler), 126 bytes
a=>{var s=new Random();int d=a.Length;return Enumerable.Range(1,s.Next(1,d)).Select(x=>a.Distinct().ToList()[s.Next(0,d-1)]);}
Try it online!
answered Apr 8 at 12:55
Expired DataExpired Data
898216
898216
add a comment |
add a comment |
$begingroup$
Forth (gforth), 68 bytes
include random.fs
: f dup random 1+ 0 do 2dup random + 1 type loop ;
Try it online!
Explanation
- Get a random number between 1 and string-length
- Loop that many times
- For each iteration
- Get a random number between 0 and string length - 1
- Add that to string starting address and output the character at that address
Code Explanation
include random.fs include the library file needed to generate random numbers
: f start a new word definition
dup duplicate the string-length
random 1+ get the length of the new string, make sure it starts from 1
0 do start a loop of that length
2dup duplicate the starting address and string length
random + get a number from 0 to string-length and add it to the address
1 type output the character at that address
loop end the loop
; end the word definition
$endgroup$
add a comment |
$begingroup$
Forth (gforth), 68 bytes
include random.fs
: f dup random 1+ 0 do 2dup random + 1 type loop ;
Try it online!
Explanation
- Get a random number between 1 and string-length
- Loop that many times
- For each iteration
- Get a random number between 0 and string length - 1
- Add that to string starting address and output the character at that address
Code Explanation
include random.fs include the library file needed to generate random numbers
: f start a new word definition
dup duplicate the string-length
random 1+ get the length of the new string, make sure it starts from 1
0 do start a loop of that length
2dup duplicate the starting address and string length
random + get a number from 0 to string-length and add it to the address
1 type output the character at that address
loop end the loop
; end the word definition
$endgroup$
add a comment |
$begingroup$
Forth (gforth), 68 bytes
include random.fs
: f dup random 1+ 0 do 2dup random + 1 type loop ;
Try it online!
Explanation
- Get a random number between 1 and string-length
- Loop that many times
- For each iteration
- Get a random number between 0 and string length - 1
- Add that to string starting address and output the character at that address
Code Explanation
include random.fs include the library file needed to generate random numbers
: f start a new word definition
dup duplicate the string-length
random 1+ get the length of the new string, make sure it starts from 1
0 do start a loop of that length
2dup duplicate the starting address and string length
random + get a number from 0 to string-length and add it to the address
1 type output the character at that address
loop end the loop
; end the word definition
$endgroup$
Forth (gforth), 68 bytes
include random.fs
: f dup random 1+ 0 do 2dup random + 1 type loop ;
Try it online!
Explanation
- Get a random number between 1 and string-length
- Loop that many times
- For each iteration
- Get a random number between 0 and string length - 1
- Add that to string starting address and output the character at that address
Code Explanation
include random.fs include the library file needed to generate random numbers
: f start a new word definition
dup duplicate the string-length
random 1+ get the length of the new string, make sure it starts from 1
0 do start a loop of that length
2dup duplicate the starting address and string length
random + get a number from 0 to string-length and add it to the address
1 type output the character at that address
loop end the loop
; end the word definition
answered Apr 8 at 13:48
reffureffu
73126
73126
add a comment |
add a comment |
$begingroup$
J, 27 bytes
[:(?@#{])@;[:,@{&.>#<@#"{<
Try it online!
standard formatting
[: (?@# { ])@; [: ,@{&.> # <@#"1 _ <
explanation
Eg, for the string 'abc' we first use #<@#"{<
to create:
┌─────┬─────────┬─────────────┐
│┌───┐│┌───┬───┐│┌───┬───┬───┐│
││abc│││abc│abc│││abc│abc│abc││
│└───┘│└───┴───┘│└───┴───┴───┘│
└─────┴─────────┴─────────────┘
We then take the cartesian product of each of these {
, flatten the results, and finally remove the outer boxing ;
.
(?@#{])@
takes a random result from that list.
$endgroup$
$begingroup$
TIO will return the same result every time, but in a normal J REPL it will be random. sounds like a bug to report, random works for Python 3 TIO...
$endgroup$
– Artemis Fowl
Apr 9 at 0:16
$begingroup$
J will start each session with the same seed for its RNG. If you keep running the same verb in a single session, you'll get different answers. But if you start your session over, the same sequence will occur. TIO is like a fresh J session. So it's not exactly a bug -- at least I wouldn't call it one.
$endgroup$
– Jonah
Apr 9 at 0:57
$begingroup$
Bug, maybe not. Improvable feature, quite possibly. As someone who's never used J, I'll leave it up to you to report the bug if you wish to.
$endgroup$
– Artemis Fowl
Apr 9 at 1:03
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
Apr 10 at 0:06
$begingroup$
The program now seems to output a list of values...
$endgroup$
– Artemis Fowl
2 days ago
|
show 2 more comments
$begingroup$
J, 27 bytes
[:(?@#{])@;[:,@{&.>#<@#"{<
Try it online!
standard formatting
[: (?@# { ])@; [: ,@{&.> # <@#"1 _ <
explanation
Eg, for the string 'abc' we first use #<@#"{<
to create:
┌─────┬─────────┬─────────────┐
│┌───┐│┌───┬───┐│┌───┬───┬───┐│
││abc│││abc│abc│││abc│abc│abc││
│└───┘│└───┴───┘│└───┴───┴───┘│
└─────┴─────────┴─────────────┘
We then take the cartesian product of each of these {
, flatten the results, and finally remove the outer boxing ;
.
(?@#{])@
takes a random result from that list.
$endgroup$
$begingroup$
TIO will return the same result every time, but in a normal J REPL it will be random. sounds like a bug to report, random works for Python 3 TIO...
$endgroup$
– Artemis Fowl
Apr 9 at 0:16
$begingroup$
J will start each session with the same seed for its RNG. If you keep running the same verb in a single session, you'll get different answers. But if you start your session over, the same sequence will occur. TIO is like a fresh J session. So it's not exactly a bug -- at least I wouldn't call it one.
$endgroup$
– Jonah
Apr 9 at 0:57
$begingroup$
Bug, maybe not. Improvable feature, quite possibly. As someone who's never used J, I'll leave it up to you to report the bug if you wish to.
$endgroup$
– Artemis Fowl
Apr 9 at 1:03
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
Apr 10 at 0:06
$begingroup$
The program now seems to output a list of values...
$endgroup$
– Artemis Fowl
2 days ago
|
show 2 more comments
$begingroup$
J, 27 bytes
[:(?@#{])@;[:,@{&.>#<@#"{<
Try it online!
standard formatting
[: (?@# { ])@; [: ,@{&.> # <@#"1 _ <
explanation
Eg, for the string 'abc' we first use #<@#"{<
to create:
┌─────┬─────────┬─────────────┐
│┌───┐│┌───┬───┐│┌───┬───┬───┐│
││abc│││abc│abc│││abc│abc│abc││
│└───┘│└───┴───┘│└───┴───┴───┘│
└─────┴─────────┴─────────────┘
We then take the cartesian product of each of these {
, flatten the results, and finally remove the outer boxing ;
.
(?@#{])@
takes a random result from that list.
$endgroup$
J, 27 bytes
[:(?@#{])@;[:,@{&.>#<@#"{<
Try it online!
standard formatting
[: (?@# { ])@; [: ,@{&.> # <@#"1 _ <
explanation
Eg, for the string 'abc' we first use #<@#"{<
to create:
┌─────┬─────────┬─────────────┐
│┌───┐│┌───┬───┐│┌───┬───┬───┐│
││abc│││abc│abc│││abc│abc│abc││
│└───┘│└───┴───┘│└───┴───┴───┘│
└─────┴─────────┴─────────────┘
We then take the cartesian product of each of these {
, flatten the results, and finally remove the outer boxing ;
.
(?@#{])@
takes a random result from that list.
edited Apr 10 at 0:06
answered Apr 6 at 19:14
JonahJonah
2,6711017
2,6711017
$begingroup$
TIO will return the same result every time, but in a normal J REPL it will be random. sounds like a bug to report, random works for Python 3 TIO...
$endgroup$
– Artemis Fowl
Apr 9 at 0:16
$begingroup$
J will start each session with the same seed for its RNG. If you keep running the same verb in a single session, you'll get different answers. But if you start your session over, the same sequence will occur. TIO is like a fresh J session. So it's not exactly a bug -- at least I wouldn't call it one.
$endgroup$
– Jonah
Apr 9 at 0:57
$begingroup$
Bug, maybe not. Improvable feature, quite possibly. As someone who's never used J, I'll leave it up to you to report the bug if you wish to.
$endgroup$
– Artemis Fowl
Apr 9 at 1:03
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
Apr 10 at 0:06
$begingroup$
The program now seems to output a list of values...
$endgroup$
– Artemis Fowl
2 days ago
|
show 2 more comments
$begingroup$
TIO will return the same result every time, but in a normal J REPL it will be random. sounds like a bug to report, random works for Python 3 TIO...
$endgroup$
– Artemis Fowl
Apr 9 at 0:16
$begingroup$
J will start each session with the same seed for its RNG. If you keep running the same verb in a single session, you'll get different answers. But if you start your session over, the same sequence will occur. TIO is like a fresh J session. So it's not exactly a bug -- at least I wouldn't call it one.
$endgroup$
– Jonah
Apr 9 at 0:57
$begingroup$
Bug, maybe not. Improvable feature, quite possibly. As someone who's never used J, I'll leave it up to you to report the bug if you wish to.
$endgroup$
– Artemis Fowl
Apr 9 at 1:03
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
Apr 10 at 0:06
$begingroup$
The program now seems to output a list of values...
$endgroup$
– Artemis Fowl
2 days ago
$begingroup$
TIO will return the same result every time, but in a normal J REPL it will be random. sounds like a bug to report, random works for Python 3 TIO...
$endgroup$
– Artemis Fowl
Apr 9 at 0:16
$begingroup$
TIO will return the same result every time, but in a normal J REPL it will be random. sounds like a bug to report, random works for Python 3 TIO...
$endgroup$
– Artemis Fowl
Apr 9 at 0:16
$begingroup$
J will start each session with the same seed for its RNG. If you keep running the same verb in a single session, you'll get different answers. But if you start your session over, the same sequence will occur. TIO is like a fresh J session. So it's not exactly a bug -- at least I wouldn't call it one.
$endgroup$
– Jonah
Apr 9 at 0:57
$begingroup$
J will start each session with the same seed for its RNG. If you keep running the same verb in a single session, you'll get different answers. But if you start your session over, the same sequence will occur. TIO is like a fresh J session. So it's not exactly a bug -- at least I wouldn't call it one.
$endgroup$
– Jonah
Apr 9 at 0:57
$begingroup$
Bug, maybe not. Improvable feature, quite possibly. As someone who's never used J, I'll leave it up to you to report the bug if you wish to.
$endgroup$
– Artemis Fowl
Apr 9 at 1:03
$begingroup$
Bug, maybe not. Improvable feature, quite possibly. As someone who's never used J, I'll leave it up to you to report the bug if you wish to.
$endgroup$
– Artemis Fowl
Apr 9 at 1:03
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
Apr 10 at 0:06
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
Apr 10 at 0:06
$begingroup$
The program now seems to output a list of values...
$endgroup$
– Artemis Fowl
2 days ago
$begingroup$
The program now seems to output a list of values...
$endgroup$
– Artemis Fowl
2 days ago
|
show 2 more comments
$begingroup$
Haskell, 143 bytes
import Control.Monad
import System.Random
f a=do
i<-randomRIO(0,length c-1)
print$c!!i
where c=concat[replicateM x a|x <-[1..length a]]
Try it online!
New contributor
$endgroup$
add a comment |
$begingroup$
Haskell, 143 bytes
import Control.Monad
import System.Random
f a=do
i<-randomRIO(0,length c-1)
print$c!!i
where c=concat[replicateM x a|x <-[1..length a]]
Try it online!
New contributor
$endgroup$
add a comment |
$begingroup$
Haskell, 143 bytes
import Control.Monad
import System.Random
f a=do
i<-randomRIO(0,length c-1)
print$c!!i
where c=concat[replicateM x a|x <-[1..length a]]
Try it online!
New contributor
$endgroup$
Haskell, 143 bytes
import Control.Monad
import System.Random
f a=do
i<-randomRIO(0,length c-1)
print$c!!i
where c=concat[replicateM x a|x <-[1..length a]]
Try it online!
New contributor
New contributor
answered 15 hours ago
bugsbugs
1314
1314
New contributor
New contributor
add a comment |
add a comment |
If this is an answer to a challenge…
…Be sure to follow the challenge specification. However, please refrain from exploiting obvious loopholes. Answers abusing any of the standard loopholes are considered invalid. If you think a specification is unclear or underspecified, comment on the question instead.
…Try to optimize your score. For instance, answers to code-golf challenges should attempt to be as short as possible. You can always include a readable version of the code in addition to the competitive one.
Explanations of your answer make it more interesting to read and are very much encouraged.…Include a short header which indicates the language(s) of your code and its score, as defined by the challenge.
More generally…
…Please make sure to answer the question and provide sufficient detail.
…Avoid asking for help, clarification or responding to other answers (use comments instead).
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f182735%2fto-string-or-not-to-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
$begingroup$
Comments are not for extended discussion; this conversation has been moved to chat.
$endgroup$
– DJMcMayhem♦
Apr 5 at 22:42
1
$begingroup$
@Flog Edoc: (2) There is a real difference between 'will contain at least two distinct characters' and 'all characters will be distinct'. Which do you intend?
$endgroup$
– Chas Brown
Apr 6 at 5:35
4
$begingroup$
This challenge seems to have been significantly changed from what was initially posted, making it a completely different challenge and invalidating the existing solutions. That's an automatic
-1
from me and the reason I've VTCed as unclear.$endgroup$
– Shaggy
Apr 7 at 11:14
2
$begingroup$
@Shaggy As it stands now, many edge cases have in fact been resolved, so it is far from unclear as compared to where it started. Right now, I think it's pretty understandable what the requirements are. Other than exact formulaic assumptions (which may affect existing answers and may as well be a different question entirely), I'm not sure what you're looking to do.
$endgroup$
– Flog Edoc
Apr 8 at 16:33
1
$begingroup$
Your three examples are all essentially the same. I suggest showing the expected output for: the empty string (if applicable — I suggest you rule it out), input
x
, inputxy
, inputxxy
, inputzzz
.$endgroup$
– Lynn
Apr 10 at 0:51