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
New contributor
$endgroup$
|
show 11 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
New contributor
$endgroup$
$begingroup$
Comments are not for extended discussion; this conversation has been moved to chat.
$endgroup$
– DJMcMayhem♦
Apr 5 at 22:42
$begingroup$
@EmbodimentofIgnorance Thanks, fixed.
$endgroup$
– Flog Edoc
Apr 6 at 0:36
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
2 days ago
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
yesterday
|
show 11 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
New contributor
$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
New contributor
New contributor
edited yesterday
Flog Edoc
New contributor
asked Apr 5 at 18:36
Flog EdocFlog Edoc
313
313
New contributor
New contributor
$begingroup$
Comments are not for extended discussion; this conversation has been moved to chat.
$endgroup$
– DJMcMayhem♦
Apr 5 at 22:42
$begingroup$
@EmbodimentofIgnorance Thanks, fixed.
$endgroup$
– Flog Edoc
Apr 6 at 0:36
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
2 days ago
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
yesterday
|
show 11 more comments
$begingroup$
Comments are not for extended discussion; this conversation has been moved to chat.
$endgroup$
– DJMcMayhem♦
Apr 5 at 22:42
$begingroup$
@EmbodimentofIgnorance Thanks, fixed.
$endgroup$
– Flog Edoc
Apr 6 at 0:36
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
2 days ago
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
yesterday
$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
$begingroup$
@EmbodimentofIgnorance Thanks, fixed.
$endgroup$
– Flog Edoc
Apr 6 at 0:36
$begingroup$
@EmbodimentofIgnorance Thanks, fixed.
$endgroup$
– Flog Edoc
Apr 6 at 0:36
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
2 days ago
$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
2 days ago
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
yesterday
$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
yesterday
|
show 11 more comments
16 Answers
16
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
2 days ago
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 bytes
sample(t<-unique(s<-scan(,"")),sample(n<-length(s),p=length(t)^(1: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$
$begingroup$
64 bytes
$endgroup$
– Giuseppe
1 hour ago
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
2 days ago
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$
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$
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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
2 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
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
});
}
});
Flog Edoc is a new contributor. Be nice, and check out our Code of Conduct.
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
16 Answers
16
active
oldest
votes
16 Answers
16
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
54k536174
54k536174
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.8k433145
47.8k433145
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
2 days ago
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
2 days ago
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 2 hours ago
answered Apr 6 at 2:31
Jo KingJo King
26.6k365132
26.6k365132
$begingroup$
33 bytes
$endgroup$
– nwellnhof
2 days ago
add a comment |
$begingroup$
33 bytes
$endgroup$
– nwellnhof
2 days ago
$begingroup$
33 bytes
$endgroup$
– nwellnhof
2 days ago
$begingroup$
33 bytes
$endgroup$
– nwellnhof
2 days ago
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,581312
1,581312
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,2191523
5,2191523
add a comment |
add a comment |
$begingroup$
R, 72 bytes
sample(t<-unique(s<-scan(,"")),sample(n<-length(s),p=length(t)^(1: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$
$begingroup$
64 bytes
$endgroup$
– Giuseppe
1 hour ago
add a comment |
$begingroup$
R, 72 bytes
sample(t<-unique(s<-scan(,"")),sample(n<-length(s),p=length(t)^(1: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$
$begingroup$
64 bytes
$endgroup$
– Giuseppe
1 hour ago
add a comment |
$begingroup$
R, 72 bytes
sample(t<-unique(s<-scan(,"")),sample(n<-length(s),p=length(t)^(1: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 bytes
sample(t<-unique(s<-scan(,"")),sample(n<-length(s),p=length(t)^(1: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 6 at 7:49
answered Apr 6 at 7:28
Robin RyderRobin Ryder
6017
6017
$begingroup$
64 bytes
$endgroup$
– Giuseppe
1 hour ago
add a comment |
$begingroup$
64 bytes
$endgroup$
– Giuseppe
1 hour ago
$begingroup$
64 bytes
$endgroup$
– Giuseppe
1 hour ago
$begingroup$
64 bytes
$endgroup$
– Giuseppe
1 hour ago
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
$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
2 days ago
$begingroup$
@Jonah
<>
is StringJoin
; ""<>#&
is an anonymous function that joins ""
with its argument (and is much shorter than StringJoin
). @*
composes functions.$endgroup$
– attinat
2 days ago
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.7k745179
82.7k745179
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,074314
2,074314
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,2191523
5,2191523
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 yesterday
Expired DataExpired Data
57314
57314
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 yesterday
reffureffu
72126
72126
add a comment |
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$
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$
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 yesterday
Ilya GazmanIlya Gazman
479313
479313
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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
2 hours ago
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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
2 hours ago
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$
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 2 hours ago
answered Apr 6 at 19:14
JonahJonah
2,6611017
2,6611017
$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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
2 hours ago
add a comment |
$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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
2 hours 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
yesterday
$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
yesterday
$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
yesterday
$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
yesterday
$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
yesterday
$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
yesterday
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
2 hours ago
$begingroup$
@ArtemisFowl I figured out how to fix it. The TIO now works, returning a fresh result each time.
$endgroup$
– Jonah
2 hours ago
add a comment |
Flog Edoc is a new contributor. Be nice, and check out our Code of Conduct.
Flog Edoc is a new contributor. Be nice, and check out our Code of Conduct.
Flog Edoc is a new contributor. Be nice, and check out our Code of Conduct.
Flog Edoc is a new contributor. Be nice, and check out our Code of Conduct.
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
$begingroup$
@EmbodimentofIgnorance Thanks, fixed.
$endgroup$
– Flog Edoc
Apr 6 at 0:36
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
2 days ago
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
yesterday