Computer Cipher











up vote
11
down vote

favorite












Introduction:



I have loads of different ciphers stored in a document I once compiled as a kid, I picked a few of the ones I thought were best suitable for challenges (not too trivial, and not too hard) and transformed them into challenges. Most of them are still in the sandbox, and I'm not sure yet whether I'll post all of them, or only a few. But here is the first of them to start things of.





A Computer Cipher will encipher the given text into 'random' character groups of a given length. If such a group contains a digit, it will use that digit to index into its own group for the enciphered character. If no digit is present in the group, it means the first character is used.



For example, let's say we want to encipher the text this is a computer cipher with a given length of 5. This is a potential output (note: numbers are 1-indexed in the example below):



t     h     i     s     i     s     a     c     o     m     p     u     t     e     r     c     i     p     h     e     r       (without spaces of course, but added as clarification)
qu5dt hprit k3iqb osyw2 jii2o m5uzs akiwb hwpc4 eoo3j muxer z4lpc 4lsuw 2tsmp eirkr r3rsi b5nvc vid2o dmh5p hrptj oeh2l 4ngrv (without spaces of course, but added as clarification)


Let's take a few groups as examples to explain how to decipher the group:





  • qu5dt: This group contains a digit 5, so the (1-indexed) 5th character of this group is the character used for the deciphered text: t.


  • hprit: This group contains no digits, so the first character of this group is used implicitly for the deciphered text: h.


  • osyw2: This groups contains a digit 2, so the (1-indexed) 2nd character of this group is the character used for the deciphered text: s.


Challenge:



Given an integer length and string word_to_encipher, output a random enciphered string as described above.



You only have to encipher given the length and word_to_encipher, so no need to create a deciphering program/function as well. I might make a part 2 challenge for the deciphering in the future however.



Challenge rules:




  • You can assume the length will be in the range [3,9].

  • You can assume the word_to_encipher will only contain letters.

  • You can use either full lowercase or full uppercase (please state which one you've used in your answer).

  • Your outputs, every group, and the positions of the digits in a group (if present) should be uniformly random. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present; and it obviously cannot be on the same position as the enciphered character).

  • You are also allowed to use 0-indexed digits instead of 1-indexed. Please state which of the two you've used in your answer.

  • The digit 1 (or 0 when 0-indexed) will never be present in the output. So b1ndh is not a valid group to encipher the character 'b'. However, b4tbw is valid, where the 4 enciphers the b at the 4th (1-indexed) position, and the other characters b,t,w are random (which coincidentally also contains a b). Other possible valid groups of length 5 to encipher the character 'b' are: abcd2, ab2de, babbk, hue5b, etc.


General rules:




  • This is code-golf, so shortest answer in bytes wins.

    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.


  • Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.


  • Default Loopholes are forbidden.

  • If possible, please add a link with a test for your code (i.e. TIO).

  • Also, adding an explanation for your answer is highly recommended.


Test cases:



Input:
Length: 5
Word to encipher: thisisacomputercipher
Possible output:
qu5dthpritk3iqbosyw2jii2om5uzsakiwbhwpc4eoo3jmuxerz4lpc4lsuw2tsmpeirkrr3rsib5nvcvid2odmh5phrptjoeh2l4ngrv

Input:
Length: 8
Word to encipher: test
Possible output:
ewetng4o6smptebyo6ontsrbtxten3qk

Input:
Length: 3
Word to encipher: three
Possible output:
tomv3h2rvege3le









share|improve this question




















  • 1




    How does "uniform" mean
    – l4m2
    Dec 3 at 9:32










  • @l4m2 That there is an equal chance for any output. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present, and also not on the same position as the enciphered character).
    – Kevin Cruijssen
    Dec 3 at 9:34












  • So abcd2, ab2de, babbk all same? Also is b1akk valid?
    – l4m2
    Dec 3 at 9:43










  • @l4m2 Yep, all three are possible outputs enciphering the character 'b'. As for b1akk I'd say no. Will edit it in the challenge description to clarify. If the first character is the enciphered one, no digit should be present.
    – Kevin Cruijssen
    Dec 3 at 9:48






  • 1




    For example, when length = 3, char = "a"; The form "a??" has 676 possible results, but "1a?", "?a1", "2?a", "?2a", has only104 results. So, if I'm trying to chose one result from all these 780 results, the distribution of "position of the enciphered letter" is 13:1:1, not 1:1:1. And I would consider this as how "uniformly random" work.
    – tsh
    2 days ago















up vote
11
down vote

favorite












Introduction:



I have loads of different ciphers stored in a document I once compiled as a kid, I picked a few of the ones I thought were best suitable for challenges (not too trivial, and not too hard) and transformed them into challenges. Most of them are still in the sandbox, and I'm not sure yet whether I'll post all of them, or only a few. But here is the first of them to start things of.





A Computer Cipher will encipher the given text into 'random' character groups of a given length. If such a group contains a digit, it will use that digit to index into its own group for the enciphered character. If no digit is present in the group, it means the first character is used.



For example, let's say we want to encipher the text this is a computer cipher with a given length of 5. This is a potential output (note: numbers are 1-indexed in the example below):



t     h     i     s     i     s     a     c     o     m     p     u     t     e     r     c     i     p     h     e     r       (without spaces of course, but added as clarification)
qu5dt hprit k3iqb osyw2 jii2o m5uzs akiwb hwpc4 eoo3j muxer z4lpc 4lsuw 2tsmp eirkr r3rsi b5nvc vid2o dmh5p hrptj oeh2l 4ngrv (without spaces of course, but added as clarification)


Let's take a few groups as examples to explain how to decipher the group:





  • qu5dt: This group contains a digit 5, so the (1-indexed) 5th character of this group is the character used for the deciphered text: t.


  • hprit: This group contains no digits, so the first character of this group is used implicitly for the deciphered text: h.


  • osyw2: This groups contains a digit 2, so the (1-indexed) 2nd character of this group is the character used for the deciphered text: s.


Challenge:



Given an integer length and string word_to_encipher, output a random enciphered string as described above.



You only have to encipher given the length and word_to_encipher, so no need to create a deciphering program/function as well. I might make a part 2 challenge for the deciphering in the future however.



Challenge rules:




  • You can assume the length will be in the range [3,9].

  • You can assume the word_to_encipher will only contain letters.

  • You can use either full lowercase or full uppercase (please state which one you've used in your answer).

  • Your outputs, every group, and the positions of the digits in a group (if present) should be uniformly random. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present; and it obviously cannot be on the same position as the enciphered character).

  • You are also allowed to use 0-indexed digits instead of 1-indexed. Please state which of the two you've used in your answer.

  • The digit 1 (or 0 when 0-indexed) will never be present in the output. So b1ndh is not a valid group to encipher the character 'b'. However, b4tbw is valid, where the 4 enciphers the b at the 4th (1-indexed) position, and the other characters b,t,w are random (which coincidentally also contains a b). Other possible valid groups of length 5 to encipher the character 'b' are: abcd2, ab2de, babbk, hue5b, etc.


General rules:




  • This is code-golf, so shortest answer in bytes wins.

    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.


  • Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.


  • Default Loopholes are forbidden.

  • If possible, please add a link with a test for your code (i.e. TIO).

  • Also, adding an explanation for your answer is highly recommended.


Test cases:



Input:
Length: 5
Word to encipher: thisisacomputercipher
Possible output:
qu5dthpritk3iqbosyw2jii2om5uzsakiwbhwpc4eoo3jmuxerz4lpc4lsuw2tsmpeirkrr3rsib5nvcvid2odmh5phrptjoeh2l4ngrv

Input:
Length: 8
Word to encipher: test
Possible output:
ewetng4o6smptebyo6ontsrbtxten3qk

Input:
Length: 3
Word to encipher: three
Possible output:
tomv3h2rvege3le









share|improve this question




















  • 1




    How does "uniform" mean
    – l4m2
    Dec 3 at 9:32










  • @l4m2 That there is an equal chance for any output. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present, and also not on the same position as the enciphered character).
    – Kevin Cruijssen
    Dec 3 at 9:34












  • So abcd2, ab2de, babbk all same? Also is b1akk valid?
    – l4m2
    Dec 3 at 9:43










  • @l4m2 Yep, all three are possible outputs enciphering the character 'b'. As for b1akk I'd say no. Will edit it in the challenge description to clarify. If the first character is the enciphered one, no digit should be present.
    – Kevin Cruijssen
    Dec 3 at 9:48






  • 1




    For example, when length = 3, char = "a"; The form "a??" has 676 possible results, but "1a?", "?a1", "2?a", "?2a", has only104 results. So, if I'm trying to chose one result from all these 780 results, the distribution of "position of the enciphered letter" is 13:1:1, not 1:1:1. And I would consider this as how "uniformly random" work.
    – tsh
    2 days ago













up vote
11
down vote

favorite









up vote
11
down vote

favorite











Introduction:



I have loads of different ciphers stored in a document I once compiled as a kid, I picked a few of the ones I thought were best suitable for challenges (not too trivial, and not too hard) and transformed them into challenges. Most of them are still in the sandbox, and I'm not sure yet whether I'll post all of them, or only a few. But here is the first of them to start things of.





A Computer Cipher will encipher the given text into 'random' character groups of a given length. If such a group contains a digit, it will use that digit to index into its own group for the enciphered character. If no digit is present in the group, it means the first character is used.



For example, let's say we want to encipher the text this is a computer cipher with a given length of 5. This is a potential output (note: numbers are 1-indexed in the example below):



t     h     i     s     i     s     a     c     o     m     p     u     t     e     r     c     i     p     h     e     r       (without spaces of course, but added as clarification)
qu5dt hprit k3iqb osyw2 jii2o m5uzs akiwb hwpc4 eoo3j muxer z4lpc 4lsuw 2tsmp eirkr r3rsi b5nvc vid2o dmh5p hrptj oeh2l 4ngrv (without spaces of course, but added as clarification)


Let's take a few groups as examples to explain how to decipher the group:





  • qu5dt: This group contains a digit 5, so the (1-indexed) 5th character of this group is the character used for the deciphered text: t.


  • hprit: This group contains no digits, so the first character of this group is used implicitly for the deciphered text: h.


  • osyw2: This groups contains a digit 2, so the (1-indexed) 2nd character of this group is the character used for the deciphered text: s.


Challenge:



Given an integer length and string word_to_encipher, output a random enciphered string as described above.



You only have to encipher given the length and word_to_encipher, so no need to create a deciphering program/function as well. I might make a part 2 challenge for the deciphering in the future however.



Challenge rules:




  • You can assume the length will be in the range [3,9].

  • You can assume the word_to_encipher will only contain letters.

  • You can use either full lowercase or full uppercase (please state which one you've used in your answer).

  • Your outputs, every group, and the positions of the digits in a group (if present) should be uniformly random. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present; and it obviously cannot be on the same position as the enciphered character).

  • You are also allowed to use 0-indexed digits instead of 1-indexed. Please state which of the two you've used in your answer.

  • The digit 1 (or 0 when 0-indexed) will never be present in the output. So b1ndh is not a valid group to encipher the character 'b'. However, b4tbw is valid, where the 4 enciphers the b at the 4th (1-indexed) position, and the other characters b,t,w are random (which coincidentally also contains a b). Other possible valid groups of length 5 to encipher the character 'b' are: abcd2, ab2de, babbk, hue5b, etc.


General rules:




  • This is code-golf, so shortest answer in bytes wins.

    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.


  • Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.


  • Default Loopholes are forbidden.

  • If possible, please add a link with a test for your code (i.e. TIO).

  • Also, adding an explanation for your answer is highly recommended.


Test cases:



Input:
Length: 5
Word to encipher: thisisacomputercipher
Possible output:
qu5dthpritk3iqbosyw2jii2om5uzsakiwbhwpc4eoo3jmuxerz4lpc4lsuw2tsmpeirkrr3rsib5nvcvid2odmh5phrptjoeh2l4ngrv

Input:
Length: 8
Word to encipher: test
Possible output:
ewetng4o6smptebyo6ontsrbtxten3qk

Input:
Length: 3
Word to encipher: three
Possible output:
tomv3h2rvege3le









share|improve this question















Introduction:



I have loads of different ciphers stored in a document I once compiled as a kid, I picked a few of the ones I thought were best suitable for challenges (not too trivial, and not too hard) and transformed them into challenges. Most of them are still in the sandbox, and I'm not sure yet whether I'll post all of them, or only a few. But here is the first of them to start things of.





A Computer Cipher will encipher the given text into 'random' character groups of a given length. If such a group contains a digit, it will use that digit to index into its own group for the enciphered character. If no digit is present in the group, it means the first character is used.



For example, let's say we want to encipher the text this is a computer cipher with a given length of 5. This is a potential output (note: numbers are 1-indexed in the example below):



t     h     i     s     i     s     a     c     o     m     p     u     t     e     r     c     i     p     h     e     r       (without spaces of course, but added as clarification)
qu5dt hprit k3iqb osyw2 jii2o m5uzs akiwb hwpc4 eoo3j muxer z4lpc 4lsuw 2tsmp eirkr r3rsi b5nvc vid2o dmh5p hrptj oeh2l 4ngrv (without spaces of course, but added as clarification)


Let's take a few groups as examples to explain how to decipher the group:





  • qu5dt: This group contains a digit 5, so the (1-indexed) 5th character of this group is the character used for the deciphered text: t.


  • hprit: This group contains no digits, so the first character of this group is used implicitly for the deciphered text: h.


  • osyw2: This groups contains a digit 2, so the (1-indexed) 2nd character of this group is the character used for the deciphered text: s.


Challenge:



Given an integer length and string word_to_encipher, output a random enciphered string as described above.



You only have to encipher given the length and word_to_encipher, so no need to create a deciphering program/function as well. I might make a part 2 challenge for the deciphering in the future however.



Challenge rules:




  • You can assume the length will be in the range [3,9].

  • You can assume the word_to_encipher will only contain letters.

  • You can use either full lowercase or full uppercase (please state which one you've used in your answer).

  • Your outputs, every group, and the positions of the digits in a group (if present) should be uniformly random. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present; and it obviously cannot be on the same position as the enciphered character).

  • You are also allowed to use 0-indexed digits instead of 1-indexed. Please state which of the two you've used in your answer.

  • The digit 1 (or 0 when 0-indexed) will never be present in the output. So b1ndh is not a valid group to encipher the character 'b'. However, b4tbw is valid, where the 4 enciphers the b at the 4th (1-indexed) position, and the other characters b,t,w are random (which coincidentally also contains a b). Other possible valid groups of length 5 to encipher the character 'b' are: abcd2, ab2de, babbk, hue5b, etc.


General rules:




  • This is code-golf, so shortest answer in bytes wins.

    Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language.


  • Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.


  • Default Loopholes are forbidden.

  • If possible, please add a link with a test for your code (i.e. TIO).

  • Also, adding an explanation for your answer is highly recommended.


Test cases:



Input:
Length: 5
Word to encipher: thisisacomputercipher
Possible output:
qu5dthpritk3iqbosyw2jii2om5uzsakiwbhwpc4eoo3jmuxerz4lpc4lsuw2tsmpeirkrr3rsib5nvcvid2odmh5phrptjoeh2l4ngrv

Input:
Length: 8
Word to encipher: test
Possible output:
ewetng4o6smptebyo6ontsrbtxten3qk

Input:
Length: 3
Word to encipher: three
Possible output:
tomv3h2rvege3le






code-golf string random cipher encoding






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 3 at 9:52

























asked Dec 3 at 9:28









Kevin Cruijssen

34.9k554184




34.9k554184








  • 1




    How does "uniform" mean
    – l4m2
    Dec 3 at 9:32










  • @l4m2 That there is an equal chance for any output. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present, and also not on the same position as the enciphered character).
    – Kevin Cruijssen
    Dec 3 at 9:34












  • So abcd2, ab2de, babbk all same? Also is b1akk valid?
    – l4m2
    Dec 3 at 9:43










  • @l4m2 Yep, all three are possible outputs enciphering the character 'b'. As for b1akk I'd say no. Will edit it in the challenge description to clarify. If the first character is the enciphered one, no digit should be present.
    – Kevin Cruijssen
    Dec 3 at 9:48






  • 1




    For example, when length = 3, char = "a"; The form "a??" has 676 possible results, but "1a?", "?a1", "2?a", "?2a", has only104 results. So, if I'm trying to chose one result from all these 780 results, the distribution of "position of the enciphered letter" is 13:1:1, not 1:1:1. And I would consider this as how "uniformly random" work.
    – tsh
    2 days ago














  • 1




    How does "uniform" mean
    – l4m2
    Dec 3 at 9:32










  • @l4m2 That there is an equal chance for any output. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present, and also not on the same position as the enciphered character).
    – Kevin Cruijssen
    Dec 3 at 9:34












  • So abcd2, ab2de, babbk all same? Also is b1akk valid?
    – l4m2
    Dec 3 at 9:43










  • @l4m2 Yep, all three are possible outputs enciphering the character 'b'. As for b1akk I'd say no. Will edit it in the challenge description to clarify. If the first character is the enciphered one, no digit should be present.
    – Kevin Cruijssen
    Dec 3 at 9:48






  • 1




    For example, when length = 3, char = "a"; The form "a??" has 676 possible results, but "1a?", "?a1", "2?a", "?2a", has only104 results. So, if I'm trying to chose one result from all these 780 results, the distribution of "position of the enciphered letter" is 13:1:1, not 1:1:1. And I would consider this as how "uniformly random" work.
    – tsh
    2 days ago








1




1




How does "uniform" mean
– l4m2
Dec 3 at 9:32




How does "uniform" mean
– l4m2
Dec 3 at 9:32












@l4m2 That there is an equal chance for any output. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present, and also not on the same position as the enciphered character).
– Kevin Cruijssen
Dec 3 at 9:34






@l4m2 That there is an equal chance for any output. So all random letters of the alphabet have the same chance of occurring; the position of the enciphered letter in each group has the same chance of occurring; and the position of the digit has the same chance of occurring (except when it's the first character and no digit is present, and also not on the same position as the enciphered character).
– Kevin Cruijssen
Dec 3 at 9:34














So abcd2, ab2de, babbk all same? Also is b1akk valid?
– l4m2
Dec 3 at 9:43




So abcd2, ab2de, babbk all same? Also is b1akk valid?
– l4m2
Dec 3 at 9:43












@l4m2 Yep, all three are possible outputs enciphering the character 'b'. As for b1akk I'd say no. Will edit it in the challenge description to clarify. If the first character is the enciphered one, no digit should be present.
– Kevin Cruijssen
Dec 3 at 9:48




@l4m2 Yep, all three are possible outputs enciphering the character 'b'. As for b1akk I'd say no. Will edit it in the challenge description to clarify. If the first character is the enciphered one, no digit should be present.
– Kevin Cruijssen
Dec 3 at 9:48




1




1




For example, when length = 3, char = "a"; The form "a??" has 676 possible results, but "1a?", "?a1", "2?a", "?2a", has only104 results. So, if I'm trying to chose one result from all these 780 results, the distribution of "position of the enciphered letter" is 13:1:1, not 1:1:1. And I would consider this as how "uniformly random" work.
– tsh
2 days ago




For example, when length = 3, char = "a"; The form "a??" has 676 possible results, but "1a?", "?a1", "2?a", "?2a", has only104 results. So, if I'm trying to chose one result from all these 780 results, the distribution of "position of the enciphered letter" is 13:1:1, not 1:1:1. And I would consider this as how "uniformly random" work.
– tsh
2 days ago










10 Answers
10






active

oldest

votes

















up vote
4
down vote














Perl 6, 125 bytes





->n{*.&{S:g{.}=(65..90)>>.chr.roll(n).join.subst(/./,$/,:th($!=roll 1..n:)).subst(/./,$!,:th($!-1??(^n+1∖$!).roll!!n+1))}}


Try it online!



Takes input and output in uppercase. Takes input curried, like f(n)(string). Uses 1 indexing.



Explanation:



->n{*.&{ ...  }}   # Anonymous code block that takes a number n and returns a function
S:g{.}= # That turns each character of the given string into
.roll(n) # Randomly pick n times with replacement
(65..90)>>.chr # From the uppercase alphabet
.join # And join
.subst( ) # Then replace
/./, ,:th($!=roll 1..n:) # A random index (saving the number in $!)
$/ # With the original character
.subst( ) # Replace again
/./,$!,:th( ... ) # The xth character with $!, where x is:
$!-1?? # If $! is not 1
(^n+1∖$!).roll # A random index that isn't $!
!!n+1 # Else an index out of range





share|improve this answer






























    up vote
    3
    down vote














    Python 2, 187 177 176 156 154 148 bytes





    lambda l,s:''.join([chr(choice(R(65,91))),c,`n`][(j==n)-(j==i)*(n>0)]for c in s for n,i in[sample(R(l),2)]for j in R(l))
    from random import*
    R=range


    Try it online!



    Uses uppercase letters, and 0-indexed numbers.



    -3 bytes, thanks to Kevin Cruijssen






    share|improve this answer























    • @KevinCruijssen Thanks :)
      – TFeld
      Dec 3 at 10:33










    • What does sample(R(l),2)[::1|-(random()<.5)] mean?
      – l4m2
      Dec 3 at 10:34










    • @l4m2 It takes 2 numbers from range(l), and shuffles them. But apparently sample does not guarantee order, so it's not needed :)
      – TFeld
      Dec 3 at 10:40










    • Can't you remove the parenthesis around (j==i)*(n>0)? The multiply has operator precedence over the subtract doesn't it?
      – Kevin Cruijssen
      Dec 3 at 10:41






    • 1




      @KevinCruijssen Yeah, I forgot to remove them, when i had some problems
      – TFeld
      Dec 3 at 10:45


















    up vote
    3
    down vote













    Pyth, 22 bytes



    smsXWJOQXmOGQJdO-UQJJz


    Try it online.



    Uses lowercase and zero-indexing.



    Explanation



    Very straightforward algorithm.



                               Implicit: read word in z
    Implicit: read number in Q
    m z For each char d in z:
    OQ Choose a number 0..Q-1
    J and call it J.
    m Q Make an array of Q
    OG random letters.
    X d Place d in this string
    J at position J.
    W If J is not 0,
    X J place J in this string
    O at a random position from
    UQ 0..Q-1
    - J except for J.
    s Concatenate the letters.
    s Concatenate the results.





    share|improve this answer






























      up vote
      3
      down vote














      JavaScript (Node.js), 135 bytes





      n=>f=([c,...s])=>c?(g=n=>n?g(--n)+(n-p?n-q|!p?(r(26)+10).toString(36):p:c):'')(n,r=n=>Math.random()*n|0,p=r(n),q=r(n-1),q+=q>=p)+f(s):s


      Try it online!



      Thank Arnauld for 1B






      share|improve this answer






























        up vote
        3
        down vote














        R, 134 132 123 bytes





        function(S,n,s=sample)for(k in utf8ToInt(S)){o=k+!1:n
        P=s(n,1)
        o[-P]=s(c(P[i<-P>1],s(17:42,n-1-i,T)))+48
        cat(intToUtf8(o))}


        Try it online!



        Takes uppercase letters.



        Explanation of old code (mostly the same approach):



        function(S,n){s=sample				# alias
        K=el(strsplit(S,"")) # split to characters
        o=1:n # output array
        for(k in K){ # for each character in the string
        P=s(n,1) # pick a Position for that character
        o[-P]= # assign to everywhere besides P:
        s( # a permutation of:
        c(P[i<-P>1], # P if it's greater than 1
        s(letters,n-1-i,T))) # and a random sample, with replacement, of lowercase letters
        o[P]=k # set k to position P
        cat(o,sep="")}} # and print





        share|improve this answer






























          up vote
          2
          down vote














          Java (JDK), 193 bytes





          s->n->s.flatMap(c->{int a=new int[n],i=n,x=0;for(;i-->0;)a[i]+=Math.random()*26+97;a[i+=Math.random()*n+1]=c;x+=Math.random()*~-n;if(i>0)a[x<i?x:x+1]=48+i;return java.util.Arrays.stream(a);})


          Try it online!




          • The index are 0-based.

          • This entry uses an IntStream (gotten through String::chars) as input, as well as a number and returns another IntStream.

          • Casts from double to int are unnecessary because of the += hack.






          share|improve this answer






























            up vote
            2
            down vote














            Japt, 29 bytes



            ;£=VöJ;CöV hUÎX hUÅÎUÎ?UÎs:Cö


            Try it online!



            Zero-indexed.



            Explanation:



            ;                                :Set C = [a...z]
            £ :For each character of the input:
            =VöJ; : Get two different random indexes from [0,length)
            CöV : Get 5 random letters
            hUÎX : Replace one at random with the character from the input
            hUÅÎ : Replace a different random character with:
            UÎ? : If the input character was not placed at 0:
            UÎs : The index of the input character
            : : Otherwise:
            Cö : A random letter
            :Implicitly join back to a string





            share|improve this answer






























              up vote
              1
              down vote














              Charcoal, 35 bytes



              NθFS«≔‽θη≔‽Φθ⁻κηζFθ¿⁼κηι¿∧η⁼κζIη‽β


              Try it online! Link is to verbose version of code. 0-indexed. Explanation:



              Nθ


              Input the length.



              FS«


              Input the word and loop over the characters.



              ≔‽θη


              Choose a random position for the deciphered letter.



              ≔‽Φθ⁻κηζ


              Choose a different random position for the digit.



              Fθ


              Loop once for each output character.



              ¿⁼κηι


              If this is the position of the deciphered letter then output it.



              ¿∧η⁼κζIη


              Otherwise if the deciphered letter is not at the beginning and this is the position of the digit then output the position of the deciphered letter.



              ‽β


              Otherwise output a random letter.






              share|improve this answer




























                up vote
                1
                down vote














                Clean, 256 bytes



                import StdEnv
                s::!Int->Int
                s _=code {
                ccall time "I:I"
                ccall srand "I:I"
                }
                r::!Int->Int
                r _=code {
                ccall rand "I:I"
                }
                $n|s 0<1#k=mape.r e rem n
                =flatten o mapc.hd[map(i|i==x=c=toChar if(i==y&&x>0)(x+48)(r i rem 26+97))[0..n-1]\x<-k[0..]&y<-k[0..]|x<>y]


                Try it online!



                Chooses:




                • a random x (position of the character in the segment)

                • a random y that isn't equal to x (position of the digit in the segment)

                • a random lowercase letter for each position not equal to x and not equal to y unless x is zero






                share|improve this answer




























                  up vote
                  0
                  down vote













                  JavaScript, 134 bytes





                  l=>w=>w.replace(/./g,c=>eval("for(s=c;!s[l-1]||s[t?t-1||9:0]!=c;t=s.replace(/\D/g,''))s=(p=Math.random()*36**l,p-p%1).toString(36)"))


                  Try it online!



                  This answer chose the encoded string from all possible encoded string uniformly. So it is more possible to make the encoded letter as the first one.






                  share|improve this answer























                    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',
                    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
                    });


                    }
                    });














                    draft saved

                    draft discarded


















                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f176931%2fcomputer-cipher%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown

























                    10 Answers
                    10






                    active

                    oldest

                    votes








                    10 Answers
                    10






                    active

                    oldest

                    votes









                    active

                    oldest

                    votes






                    active

                    oldest

                    votes








                    up vote
                    4
                    down vote














                    Perl 6, 125 bytes





                    ->n{*.&{S:g{.}=(65..90)>>.chr.roll(n).join.subst(/./,$/,:th($!=roll 1..n:)).subst(/./,$!,:th($!-1??(^n+1∖$!).roll!!n+1))}}


                    Try it online!



                    Takes input and output in uppercase. Takes input curried, like f(n)(string). Uses 1 indexing.



                    Explanation:



                    ->n{*.&{ ...  }}   # Anonymous code block that takes a number n and returns a function
                    S:g{.}= # That turns each character of the given string into
                    .roll(n) # Randomly pick n times with replacement
                    (65..90)>>.chr # From the uppercase alphabet
                    .join # And join
                    .subst( ) # Then replace
                    /./, ,:th($!=roll 1..n:) # A random index (saving the number in $!)
                    $/ # With the original character
                    .subst( ) # Replace again
                    /./,$!,:th( ... ) # The xth character with $!, where x is:
                    $!-1?? # If $! is not 1
                    (^n+1∖$!).roll # A random index that isn't $!
                    !!n+1 # Else an index out of range





                    share|improve this answer



























                      up vote
                      4
                      down vote














                      Perl 6, 125 bytes





                      ->n{*.&{S:g{.}=(65..90)>>.chr.roll(n).join.subst(/./,$/,:th($!=roll 1..n:)).subst(/./,$!,:th($!-1??(^n+1∖$!).roll!!n+1))}}


                      Try it online!



                      Takes input and output in uppercase. Takes input curried, like f(n)(string). Uses 1 indexing.



                      Explanation:



                      ->n{*.&{ ...  }}   # Anonymous code block that takes a number n and returns a function
                      S:g{.}= # That turns each character of the given string into
                      .roll(n) # Randomly pick n times with replacement
                      (65..90)>>.chr # From the uppercase alphabet
                      .join # And join
                      .subst( ) # Then replace
                      /./, ,:th($!=roll 1..n:) # A random index (saving the number in $!)
                      $/ # With the original character
                      .subst( ) # Replace again
                      /./,$!,:th( ... ) # The xth character with $!, where x is:
                      $!-1?? # If $! is not 1
                      (^n+1∖$!).roll # A random index that isn't $!
                      !!n+1 # Else an index out of range





                      share|improve this answer

























                        up vote
                        4
                        down vote










                        up vote
                        4
                        down vote










                        Perl 6, 125 bytes





                        ->n{*.&{S:g{.}=(65..90)>>.chr.roll(n).join.subst(/./,$/,:th($!=roll 1..n:)).subst(/./,$!,:th($!-1??(^n+1∖$!).roll!!n+1))}}


                        Try it online!



                        Takes input and output in uppercase. Takes input curried, like f(n)(string). Uses 1 indexing.



                        Explanation:



                        ->n{*.&{ ...  }}   # Anonymous code block that takes a number n and returns a function
                        S:g{.}= # That turns each character of the given string into
                        .roll(n) # Randomly pick n times with replacement
                        (65..90)>>.chr # From the uppercase alphabet
                        .join # And join
                        .subst( ) # Then replace
                        /./, ,:th($!=roll 1..n:) # A random index (saving the number in $!)
                        $/ # With the original character
                        .subst( ) # Replace again
                        /./,$!,:th( ... ) # The xth character with $!, where x is:
                        $!-1?? # If $! is not 1
                        (^n+1∖$!).roll # A random index that isn't $!
                        !!n+1 # Else an index out of range





                        share|improve this answer















                        Perl 6, 125 bytes





                        ->n{*.&{S:g{.}=(65..90)>>.chr.roll(n).join.subst(/./,$/,:th($!=roll 1..n:)).subst(/./,$!,:th($!-1??(^n+1∖$!).roll!!n+1))}}


                        Try it online!



                        Takes input and output in uppercase. Takes input curried, like f(n)(string). Uses 1 indexing.



                        Explanation:



                        ->n{*.&{ ...  }}   # Anonymous code block that takes a number n and returns a function
                        S:g{.}= # That turns each character of the given string into
                        .roll(n) # Randomly pick n times with replacement
                        (65..90)>>.chr # From the uppercase alphabet
                        .join # And join
                        .subst( ) # Then replace
                        /./, ,:th($!=roll 1..n:) # A random index (saving the number in $!)
                        $/ # With the original character
                        .subst( ) # Replace again
                        /./,$!,:th( ... ) # The xth character with $!, where x is:
                        $!-1?? # If $! is not 1
                        (^n+1∖$!).roll # A random index that isn't $!
                        !!n+1 # Else an index out of range






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Dec 3 at 11:02

























                        answered Dec 3 at 10:46









                        Jo King

                        19.9k245105




                        19.9k245105






















                            up vote
                            3
                            down vote














                            Python 2, 187 177 176 156 154 148 bytes





                            lambda l,s:''.join([chr(choice(R(65,91))),c,`n`][(j==n)-(j==i)*(n>0)]for c in s for n,i in[sample(R(l),2)]for j in R(l))
                            from random import*
                            R=range


                            Try it online!



                            Uses uppercase letters, and 0-indexed numbers.



                            -3 bytes, thanks to Kevin Cruijssen






                            share|improve this answer























                            • @KevinCruijssen Thanks :)
                              – TFeld
                              Dec 3 at 10:33










                            • What does sample(R(l),2)[::1|-(random()<.5)] mean?
                              – l4m2
                              Dec 3 at 10:34










                            • @l4m2 It takes 2 numbers from range(l), and shuffles them. But apparently sample does not guarantee order, so it's not needed :)
                              – TFeld
                              Dec 3 at 10:40










                            • Can't you remove the parenthesis around (j==i)*(n>0)? The multiply has operator precedence over the subtract doesn't it?
                              – Kevin Cruijssen
                              Dec 3 at 10:41






                            • 1




                              @KevinCruijssen Yeah, I forgot to remove them, when i had some problems
                              – TFeld
                              Dec 3 at 10:45















                            up vote
                            3
                            down vote














                            Python 2, 187 177 176 156 154 148 bytes





                            lambda l,s:''.join([chr(choice(R(65,91))),c,`n`][(j==n)-(j==i)*(n>0)]for c in s for n,i in[sample(R(l),2)]for j in R(l))
                            from random import*
                            R=range


                            Try it online!



                            Uses uppercase letters, and 0-indexed numbers.



                            -3 bytes, thanks to Kevin Cruijssen






                            share|improve this answer























                            • @KevinCruijssen Thanks :)
                              – TFeld
                              Dec 3 at 10:33










                            • What does sample(R(l),2)[::1|-(random()<.5)] mean?
                              – l4m2
                              Dec 3 at 10:34










                            • @l4m2 It takes 2 numbers from range(l), and shuffles them. But apparently sample does not guarantee order, so it's not needed :)
                              – TFeld
                              Dec 3 at 10:40










                            • Can't you remove the parenthesis around (j==i)*(n>0)? The multiply has operator precedence over the subtract doesn't it?
                              – Kevin Cruijssen
                              Dec 3 at 10:41






                            • 1




                              @KevinCruijssen Yeah, I forgot to remove them, when i had some problems
                              – TFeld
                              Dec 3 at 10:45













                            up vote
                            3
                            down vote










                            up vote
                            3
                            down vote










                            Python 2, 187 177 176 156 154 148 bytes





                            lambda l,s:''.join([chr(choice(R(65,91))),c,`n`][(j==n)-(j==i)*(n>0)]for c in s for n,i in[sample(R(l),2)]for j in R(l))
                            from random import*
                            R=range


                            Try it online!



                            Uses uppercase letters, and 0-indexed numbers.



                            -3 bytes, thanks to Kevin Cruijssen






                            share|improve this answer















                            Python 2, 187 177 176 156 154 148 bytes





                            lambda l,s:''.join([chr(choice(R(65,91))),c,`n`][(j==n)-(j==i)*(n>0)]for c in s for n,i in[sample(R(l),2)]for j in R(l))
                            from random import*
                            R=range


                            Try it online!



                            Uses uppercase letters, and 0-indexed numbers.



                            -3 bytes, thanks to Kevin Cruijssen







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Dec 3 at 10:52

























                            answered Dec 3 at 10:15









                            TFeld

                            13.8k21239




                            13.8k21239












                            • @KevinCruijssen Thanks :)
                              – TFeld
                              Dec 3 at 10:33










                            • What does sample(R(l),2)[::1|-(random()<.5)] mean?
                              – l4m2
                              Dec 3 at 10:34










                            • @l4m2 It takes 2 numbers from range(l), and shuffles them. But apparently sample does not guarantee order, so it's not needed :)
                              – TFeld
                              Dec 3 at 10:40










                            • Can't you remove the parenthesis around (j==i)*(n>0)? The multiply has operator precedence over the subtract doesn't it?
                              – Kevin Cruijssen
                              Dec 3 at 10:41






                            • 1




                              @KevinCruijssen Yeah, I forgot to remove them, when i had some problems
                              – TFeld
                              Dec 3 at 10:45


















                            • @KevinCruijssen Thanks :)
                              – TFeld
                              Dec 3 at 10:33










                            • What does sample(R(l),2)[::1|-(random()<.5)] mean?
                              – l4m2
                              Dec 3 at 10:34










                            • @l4m2 It takes 2 numbers from range(l), and shuffles them. But apparently sample does not guarantee order, so it's not needed :)
                              – TFeld
                              Dec 3 at 10:40










                            • Can't you remove the parenthesis around (j==i)*(n>0)? The multiply has operator precedence over the subtract doesn't it?
                              – Kevin Cruijssen
                              Dec 3 at 10:41






                            • 1




                              @KevinCruijssen Yeah, I forgot to remove them, when i had some problems
                              – TFeld
                              Dec 3 at 10:45
















                            @KevinCruijssen Thanks :)
                            – TFeld
                            Dec 3 at 10:33




                            @KevinCruijssen Thanks :)
                            – TFeld
                            Dec 3 at 10:33












                            What does sample(R(l),2)[::1|-(random()<.5)] mean?
                            – l4m2
                            Dec 3 at 10:34




                            What does sample(R(l),2)[::1|-(random()<.5)] mean?
                            – l4m2
                            Dec 3 at 10:34












                            @l4m2 It takes 2 numbers from range(l), and shuffles them. But apparently sample does not guarantee order, so it's not needed :)
                            – TFeld
                            Dec 3 at 10:40




                            @l4m2 It takes 2 numbers from range(l), and shuffles them. But apparently sample does not guarantee order, so it's not needed :)
                            – TFeld
                            Dec 3 at 10:40












                            Can't you remove the parenthesis around (j==i)*(n>0)? The multiply has operator precedence over the subtract doesn't it?
                            – Kevin Cruijssen
                            Dec 3 at 10:41




                            Can't you remove the parenthesis around (j==i)*(n>0)? The multiply has operator precedence over the subtract doesn't it?
                            – Kevin Cruijssen
                            Dec 3 at 10:41




                            1




                            1




                            @KevinCruijssen Yeah, I forgot to remove them, when i had some problems
                            – TFeld
                            Dec 3 at 10:45




                            @KevinCruijssen Yeah, I forgot to remove them, when i had some problems
                            – TFeld
                            Dec 3 at 10:45










                            up vote
                            3
                            down vote













                            Pyth, 22 bytes



                            smsXWJOQXmOGQJdO-UQJJz


                            Try it online.



                            Uses lowercase and zero-indexing.



                            Explanation



                            Very straightforward algorithm.



                                                       Implicit: read word in z
                            Implicit: read number in Q
                            m z For each char d in z:
                            OQ Choose a number 0..Q-1
                            J and call it J.
                            m Q Make an array of Q
                            OG random letters.
                            X d Place d in this string
                            J at position J.
                            W If J is not 0,
                            X J place J in this string
                            O at a random position from
                            UQ 0..Q-1
                            - J except for J.
                            s Concatenate the letters.
                            s Concatenate the results.





                            share|improve this answer



























                              up vote
                              3
                              down vote













                              Pyth, 22 bytes



                              smsXWJOQXmOGQJdO-UQJJz


                              Try it online.



                              Uses lowercase and zero-indexing.



                              Explanation



                              Very straightforward algorithm.



                                                         Implicit: read word in z
                              Implicit: read number in Q
                              m z For each char d in z:
                              OQ Choose a number 0..Q-1
                              J and call it J.
                              m Q Make an array of Q
                              OG random letters.
                              X d Place d in this string
                              J at position J.
                              W If J is not 0,
                              X J place J in this string
                              O at a random position from
                              UQ 0..Q-1
                              - J except for J.
                              s Concatenate the letters.
                              s Concatenate the results.





                              share|improve this answer

























                                up vote
                                3
                                down vote










                                up vote
                                3
                                down vote









                                Pyth, 22 bytes



                                smsXWJOQXmOGQJdO-UQJJz


                                Try it online.



                                Uses lowercase and zero-indexing.



                                Explanation



                                Very straightforward algorithm.



                                                           Implicit: read word in z
                                Implicit: read number in Q
                                m z For each char d in z:
                                OQ Choose a number 0..Q-1
                                J and call it J.
                                m Q Make an array of Q
                                OG random letters.
                                X d Place d in this string
                                J at position J.
                                W If J is not 0,
                                X J place J in this string
                                O at a random position from
                                UQ 0..Q-1
                                - J except for J.
                                s Concatenate the letters.
                                s Concatenate the results.





                                share|improve this answer














                                Pyth, 22 bytes



                                smsXWJOQXmOGQJdO-UQJJz


                                Try it online.



                                Uses lowercase and zero-indexing.



                                Explanation



                                Very straightforward algorithm.



                                                           Implicit: read word in z
                                Implicit: read number in Q
                                m z For each char d in z:
                                OQ Choose a number 0..Q-1
                                J and call it J.
                                m Q Make an array of Q
                                OG random letters.
                                X d Place d in this string
                                J at position J.
                                W If J is not 0,
                                X J place J in this string
                                O at a random position from
                                UQ 0..Q-1
                                - J except for J.
                                s Concatenate the letters.
                                s Concatenate the results.






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Dec 3 at 17:00

























                                answered Dec 3 at 13:34









                                Pietu1998

                                15.5k22780




                                15.5k22780






















                                    up vote
                                    3
                                    down vote














                                    JavaScript (Node.js), 135 bytes





                                    n=>f=([c,...s])=>c?(g=n=>n?g(--n)+(n-p?n-q|!p?(r(26)+10).toString(36):p:c):'')(n,r=n=>Math.random()*n|0,p=r(n),q=r(n-1),q+=q>=p)+f(s):s


                                    Try it online!



                                    Thank Arnauld for 1B






                                    share|improve this answer



























                                      up vote
                                      3
                                      down vote














                                      JavaScript (Node.js), 135 bytes





                                      n=>f=([c,...s])=>c?(g=n=>n?g(--n)+(n-p?n-q|!p?(r(26)+10).toString(36):p:c):'')(n,r=n=>Math.random()*n|0,p=r(n),q=r(n-1),q+=q>=p)+f(s):s


                                      Try it online!



                                      Thank Arnauld for 1B






                                      share|improve this answer

























                                        up vote
                                        3
                                        down vote










                                        up vote
                                        3
                                        down vote










                                        JavaScript (Node.js), 135 bytes





                                        n=>f=([c,...s])=>c?(g=n=>n?g(--n)+(n-p?n-q|!p?(r(26)+10).toString(36):p:c):'')(n,r=n=>Math.random()*n|0,p=r(n),q=r(n-1),q+=q>=p)+f(s):s


                                        Try it online!



                                        Thank Arnauld for 1B






                                        share|improve this answer















                                        JavaScript (Node.js), 135 bytes





                                        n=>f=([c,...s])=>c?(g=n=>n?g(--n)+(n-p?n-q|!p?(r(26)+10).toString(36):p:c):'')(n,r=n=>Math.random()*n|0,p=r(n),q=r(n-1),q+=q>=p)+f(s):s


                                        Try it online!



                                        Thank Arnauld for 1B







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Dec 3 at 17:06

























                                        answered Dec 3 at 10:15









                                        l4m2

                                        4,5111634




                                        4,5111634






















                                            up vote
                                            3
                                            down vote














                                            R, 134 132 123 bytes





                                            function(S,n,s=sample)for(k in utf8ToInt(S)){o=k+!1:n
                                            P=s(n,1)
                                            o[-P]=s(c(P[i<-P>1],s(17:42,n-1-i,T)))+48
                                            cat(intToUtf8(o))}


                                            Try it online!



                                            Takes uppercase letters.



                                            Explanation of old code (mostly the same approach):



                                            function(S,n){s=sample				# alias
                                            K=el(strsplit(S,"")) # split to characters
                                            o=1:n # output array
                                            for(k in K){ # for each character in the string
                                            P=s(n,1) # pick a Position for that character
                                            o[-P]= # assign to everywhere besides P:
                                            s( # a permutation of:
                                            c(P[i<-P>1], # P if it's greater than 1
                                            s(letters,n-1-i,T))) # and a random sample, with replacement, of lowercase letters
                                            o[P]=k # set k to position P
                                            cat(o,sep="")}} # and print





                                            share|improve this answer



























                                              up vote
                                              3
                                              down vote














                                              R, 134 132 123 bytes





                                              function(S,n,s=sample)for(k in utf8ToInt(S)){o=k+!1:n
                                              P=s(n,1)
                                              o[-P]=s(c(P[i<-P>1],s(17:42,n-1-i,T)))+48
                                              cat(intToUtf8(o))}


                                              Try it online!



                                              Takes uppercase letters.



                                              Explanation of old code (mostly the same approach):



                                              function(S,n){s=sample				# alias
                                              K=el(strsplit(S,"")) # split to characters
                                              o=1:n # output array
                                              for(k in K){ # for each character in the string
                                              P=s(n,1) # pick a Position for that character
                                              o[-P]= # assign to everywhere besides P:
                                              s( # a permutation of:
                                              c(P[i<-P>1], # P if it's greater than 1
                                              s(letters,n-1-i,T))) # and a random sample, with replacement, of lowercase letters
                                              o[P]=k # set k to position P
                                              cat(o,sep="")}} # and print





                                              share|improve this answer

























                                                up vote
                                                3
                                                down vote










                                                up vote
                                                3
                                                down vote










                                                R, 134 132 123 bytes





                                                function(S,n,s=sample)for(k in utf8ToInt(S)){o=k+!1:n
                                                P=s(n,1)
                                                o[-P]=s(c(P[i<-P>1],s(17:42,n-1-i,T)))+48
                                                cat(intToUtf8(o))}


                                                Try it online!



                                                Takes uppercase letters.



                                                Explanation of old code (mostly the same approach):



                                                function(S,n){s=sample				# alias
                                                K=el(strsplit(S,"")) # split to characters
                                                o=1:n # output array
                                                for(k in K){ # for each character in the string
                                                P=s(n,1) # pick a Position for that character
                                                o[-P]= # assign to everywhere besides P:
                                                s( # a permutation of:
                                                c(P[i<-P>1], # P if it's greater than 1
                                                s(letters,n-1-i,T))) # and a random sample, with replacement, of lowercase letters
                                                o[P]=k # set k to position P
                                                cat(o,sep="")}} # and print





                                                share|improve this answer















                                                R, 134 132 123 bytes





                                                function(S,n,s=sample)for(k in utf8ToInt(S)){o=k+!1:n
                                                P=s(n,1)
                                                o[-P]=s(c(P[i<-P>1],s(17:42,n-1-i,T)))+48
                                                cat(intToUtf8(o))}


                                                Try it online!



                                                Takes uppercase letters.



                                                Explanation of old code (mostly the same approach):



                                                function(S,n){s=sample				# alias
                                                K=el(strsplit(S,"")) # split to characters
                                                o=1:n # output array
                                                for(k in K){ # for each character in the string
                                                P=s(n,1) # pick a Position for that character
                                                o[-P]= # assign to everywhere besides P:
                                                s( # a permutation of:
                                                c(P[i<-P>1], # P if it's greater than 1
                                                s(letters,n-1-i,T))) # and a random sample, with replacement, of lowercase letters
                                                o[P]=k # set k to position P
                                                cat(o,sep="")}} # and print






                                                share|improve this answer














                                                share|improve this answer



                                                share|improve this answer








                                                edited 2 days ago

























                                                answered 2 days ago









                                                Giuseppe

                                                16.2k31052




                                                16.2k31052






















                                                    up vote
                                                    2
                                                    down vote














                                                    Java (JDK), 193 bytes





                                                    s->n->s.flatMap(c->{int a=new int[n],i=n,x=0;for(;i-->0;)a[i]+=Math.random()*26+97;a[i+=Math.random()*n+1]=c;x+=Math.random()*~-n;if(i>0)a[x<i?x:x+1]=48+i;return java.util.Arrays.stream(a);})


                                                    Try it online!




                                                    • The index are 0-based.

                                                    • This entry uses an IntStream (gotten through String::chars) as input, as well as a number and returns another IntStream.

                                                    • Casts from double to int are unnecessary because of the += hack.






                                                    share|improve this answer



























                                                      up vote
                                                      2
                                                      down vote














                                                      Java (JDK), 193 bytes





                                                      s->n->s.flatMap(c->{int a=new int[n],i=n,x=0;for(;i-->0;)a[i]+=Math.random()*26+97;a[i+=Math.random()*n+1]=c;x+=Math.random()*~-n;if(i>0)a[x<i?x:x+1]=48+i;return java.util.Arrays.stream(a);})


                                                      Try it online!




                                                      • The index are 0-based.

                                                      • This entry uses an IntStream (gotten through String::chars) as input, as well as a number and returns another IntStream.

                                                      • Casts from double to int are unnecessary because of the += hack.






                                                      share|improve this answer

























                                                        up vote
                                                        2
                                                        down vote










                                                        up vote
                                                        2
                                                        down vote










                                                        Java (JDK), 193 bytes





                                                        s->n->s.flatMap(c->{int a=new int[n],i=n,x=0;for(;i-->0;)a[i]+=Math.random()*26+97;a[i+=Math.random()*n+1]=c;x+=Math.random()*~-n;if(i>0)a[x<i?x:x+1]=48+i;return java.util.Arrays.stream(a);})


                                                        Try it online!




                                                        • The index are 0-based.

                                                        • This entry uses an IntStream (gotten through String::chars) as input, as well as a number and returns another IntStream.

                                                        • Casts from double to int are unnecessary because of the += hack.






                                                        share|improve this answer















                                                        Java (JDK), 193 bytes





                                                        s->n->s.flatMap(c->{int a=new int[n],i=n,x=0;for(;i-->0;)a[i]+=Math.random()*26+97;a[i+=Math.random()*n+1]=c;x+=Math.random()*~-n;if(i>0)a[x<i?x:x+1]=48+i;return java.util.Arrays.stream(a);})


                                                        Try it online!




                                                        • The index are 0-based.

                                                        • This entry uses an IntStream (gotten through String::chars) as input, as well as a number and returns another IntStream.

                                                        • Casts from double to int are unnecessary because of the += hack.







                                                        share|improve this answer














                                                        share|improve this answer



                                                        share|improve this answer








                                                        edited yesterday

























                                                        answered Dec 3 at 16:30









                                                        Olivier Grégoire

                                                        8,47711843




                                                        8,47711843






















                                                            up vote
                                                            2
                                                            down vote














                                                            Japt, 29 bytes



                                                            ;£=VöJ;CöV hUÎX hUÅÎUÎ?UÎs:Cö


                                                            Try it online!



                                                            Zero-indexed.



                                                            Explanation:



                                                            ;                                :Set C = [a...z]
                                                            £ :For each character of the input:
                                                            =VöJ; : Get two different random indexes from [0,length)
                                                            CöV : Get 5 random letters
                                                            hUÎX : Replace one at random with the character from the input
                                                            hUÅÎ : Replace a different random character with:
                                                            UÎ? : If the input character was not placed at 0:
                                                            UÎs : The index of the input character
                                                            : : Otherwise:
                                                            Cö : A random letter
                                                            :Implicitly join back to a string





                                                            share|improve this answer



























                                                              up vote
                                                              2
                                                              down vote














                                                              Japt, 29 bytes



                                                              ;£=VöJ;CöV hUÎX hUÅÎUÎ?UÎs:Cö


                                                              Try it online!



                                                              Zero-indexed.



                                                              Explanation:



                                                              ;                                :Set C = [a...z]
                                                              £ :For each character of the input:
                                                              =VöJ; : Get two different random indexes from [0,length)
                                                              CöV : Get 5 random letters
                                                              hUÎX : Replace one at random with the character from the input
                                                              hUÅÎ : Replace a different random character with:
                                                              UÎ? : If the input character was not placed at 0:
                                                              UÎs : The index of the input character
                                                              : : Otherwise:
                                                              Cö : A random letter
                                                              :Implicitly join back to a string





                                                              share|improve this answer

























                                                                up vote
                                                                2
                                                                down vote










                                                                up vote
                                                                2
                                                                down vote










                                                                Japt, 29 bytes



                                                                ;£=VöJ;CöV hUÎX hUÅÎUÎ?UÎs:Cö


                                                                Try it online!



                                                                Zero-indexed.



                                                                Explanation:



                                                                ;                                :Set C = [a...z]
                                                                £ :For each character of the input:
                                                                =VöJ; : Get two different random indexes from [0,length)
                                                                CöV : Get 5 random letters
                                                                hUÎX : Replace one at random with the character from the input
                                                                hUÅÎ : Replace a different random character with:
                                                                UÎ? : If the input character was not placed at 0:
                                                                UÎs : The index of the input character
                                                                : : Otherwise:
                                                                Cö : A random letter
                                                                :Implicitly join back to a string





                                                                share|improve this answer















                                                                Japt, 29 bytes



                                                                ;£=VöJ;CöV hUÎX hUÅÎUÎ?UÎs:Cö


                                                                Try it online!



                                                                Zero-indexed.



                                                                Explanation:



                                                                ;                                :Set C = [a...z]
                                                                £ :For each character of the input:
                                                                =VöJ; : Get two different random indexes from [0,length)
                                                                CöV : Get 5 random letters
                                                                hUÎX : Replace one at random with the character from the input
                                                                hUÅÎ : Replace a different random character with:
                                                                UÎ? : If the input character was not placed at 0:
                                                                UÎs : The index of the input character
                                                                : : Otherwise:
                                                                Cö : A random letter
                                                                :Implicitly join back to a string






                                                                share|improve this answer














                                                                share|improve this answer



                                                                share|improve this answer








                                                                edited yesterday

























                                                                answered Dec 3 at 16:16









                                                                Kamil Drakari

                                                                2,686416




                                                                2,686416






















                                                                    up vote
                                                                    1
                                                                    down vote














                                                                    Charcoal, 35 bytes



                                                                    NθFS«≔‽θη≔‽Φθ⁻κηζFθ¿⁼κηι¿∧η⁼κζIη‽β


                                                                    Try it online! Link is to verbose version of code. 0-indexed. Explanation:



                                                                    Nθ


                                                                    Input the length.



                                                                    FS«


                                                                    Input the word and loop over the characters.



                                                                    ≔‽θη


                                                                    Choose a random position for the deciphered letter.



                                                                    ≔‽Φθ⁻κηζ


                                                                    Choose a different random position for the digit.



                                                                    Fθ


                                                                    Loop once for each output character.



                                                                    ¿⁼κηι


                                                                    If this is the position of the deciphered letter then output it.



                                                                    ¿∧η⁼κζIη


                                                                    Otherwise if the deciphered letter is not at the beginning and this is the position of the digit then output the position of the deciphered letter.



                                                                    ‽β


                                                                    Otherwise output a random letter.






                                                                    share|improve this answer

























                                                                      up vote
                                                                      1
                                                                      down vote














                                                                      Charcoal, 35 bytes



                                                                      NθFS«≔‽θη≔‽Φθ⁻κηζFθ¿⁼κηι¿∧η⁼κζIη‽β


                                                                      Try it online! Link is to verbose version of code. 0-indexed. Explanation:



                                                                      Nθ


                                                                      Input the length.



                                                                      FS«


                                                                      Input the word and loop over the characters.



                                                                      ≔‽θη


                                                                      Choose a random position for the deciphered letter.



                                                                      ≔‽Φθ⁻κηζ


                                                                      Choose a different random position for the digit.



                                                                      Fθ


                                                                      Loop once for each output character.



                                                                      ¿⁼κηι


                                                                      If this is the position of the deciphered letter then output it.



                                                                      ¿∧η⁼κζIη


                                                                      Otherwise if the deciphered letter is not at the beginning and this is the position of the digit then output the position of the deciphered letter.



                                                                      ‽β


                                                                      Otherwise output a random letter.






                                                                      share|improve this answer























                                                                        up vote
                                                                        1
                                                                        down vote










                                                                        up vote
                                                                        1
                                                                        down vote










                                                                        Charcoal, 35 bytes



                                                                        NθFS«≔‽θη≔‽Φθ⁻κηζFθ¿⁼κηι¿∧η⁼κζIη‽β


                                                                        Try it online! Link is to verbose version of code. 0-indexed. Explanation:



                                                                        Nθ


                                                                        Input the length.



                                                                        FS«


                                                                        Input the word and loop over the characters.



                                                                        ≔‽θη


                                                                        Choose a random position for the deciphered letter.



                                                                        ≔‽Φθ⁻κηζ


                                                                        Choose a different random position for the digit.



                                                                        Fθ


                                                                        Loop once for each output character.



                                                                        ¿⁼κηι


                                                                        If this is the position of the deciphered letter then output it.



                                                                        ¿∧η⁼κζIη


                                                                        Otherwise if the deciphered letter is not at the beginning and this is the position of the digit then output the position of the deciphered letter.



                                                                        ‽β


                                                                        Otherwise output a random letter.






                                                                        share|improve this answer













                                                                        Charcoal, 35 bytes



                                                                        NθFS«≔‽θη≔‽Φθ⁻κηζFθ¿⁼κηι¿∧η⁼κζIη‽β


                                                                        Try it online! Link is to verbose version of code. 0-indexed. Explanation:



                                                                        Nθ


                                                                        Input the length.



                                                                        FS«


                                                                        Input the word and loop over the characters.



                                                                        ≔‽θη


                                                                        Choose a random position for the deciphered letter.



                                                                        ≔‽Φθ⁻κηζ


                                                                        Choose a different random position for the digit.



                                                                        Fθ


                                                                        Loop once for each output character.



                                                                        ¿⁼κηι


                                                                        If this is the position of the deciphered letter then output it.



                                                                        ¿∧η⁼κζIη


                                                                        Otherwise if the deciphered letter is not at the beginning and this is the position of the digit then output the position of the deciphered letter.



                                                                        ‽β


                                                                        Otherwise output a random letter.







                                                                        share|improve this answer












                                                                        share|improve this answer



                                                                        share|improve this answer










                                                                        answered Dec 3 at 22:35









                                                                        Neil

                                                                        78.5k744175




                                                                        78.5k744175






















                                                                            up vote
                                                                            1
                                                                            down vote














                                                                            Clean, 256 bytes



                                                                            import StdEnv
                                                                            s::!Int->Int
                                                                            s _=code {
                                                                            ccall time "I:I"
                                                                            ccall srand "I:I"
                                                                            }
                                                                            r::!Int->Int
                                                                            r _=code {
                                                                            ccall rand "I:I"
                                                                            }
                                                                            $n|s 0<1#k=mape.r e rem n
                                                                            =flatten o mapc.hd[map(i|i==x=c=toChar if(i==y&&x>0)(x+48)(r i rem 26+97))[0..n-1]\x<-k[0..]&y<-k[0..]|x<>y]


                                                                            Try it online!



                                                                            Chooses:




                                                                            • a random x (position of the character in the segment)

                                                                            • a random y that isn't equal to x (position of the digit in the segment)

                                                                            • a random lowercase letter for each position not equal to x and not equal to y unless x is zero






                                                                            share|improve this answer

























                                                                              up vote
                                                                              1
                                                                              down vote














                                                                              Clean, 256 bytes



                                                                              import StdEnv
                                                                              s::!Int->Int
                                                                              s _=code {
                                                                              ccall time "I:I"
                                                                              ccall srand "I:I"
                                                                              }
                                                                              r::!Int->Int
                                                                              r _=code {
                                                                              ccall rand "I:I"
                                                                              }
                                                                              $n|s 0<1#k=mape.r e rem n
                                                                              =flatten o mapc.hd[map(i|i==x=c=toChar if(i==y&&x>0)(x+48)(r i rem 26+97))[0..n-1]\x<-k[0..]&y<-k[0..]|x<>y]


                                                                              Try it online!



                                                                              Chooses:




                                                                              • a random x (position of the character in the segment)

                                                                              • a random y that isn't equal to x (position of the digit in the segment)

                                                                              • a random lowercase letter for each position not equal to x and not equal to y unless x is zero






                                                                              share|improve this answer























                                                                                up vote
                                                                                1
                                                                                down vote










                                                                                up vote
                                                                                1
                                                                                down vote










                                                                                Clean, 256 bytes



                                                                                import StdEnv
                                                                                s::!Int->Int
                                                                                s _=code {
                                                                                ccall time "I:I"
                                                                                ccall srand "I:I"
                                                                                }
                                                                                r::!Int->Int
                                                                                r _=code {
                                                                                ccall rand "I:I"
                                                                                }
                                                                                $n|s 0<1#k=mape.r e rem n
                                                                                =flatten o mapc.hd[map(i|i==x=c=toChar if(i==y&&x>0)(x+48)(r i rem 26+97))[0..n-1]\x<-k[0..]&y<-k[0..]|x<>y]


                                                                                Try it online!



                                                                                Chooses:




                                                                                • a random x (position of the character in the segment)

                                                                                • a random y that isn't equal to x (position of the digit in the segment)

                                                                                • a random lowercase letter for each position not equal to x and not equal to y unless x is zero






                                                                                share|improve this answer













                                                                                Clean, 256 bytes



                                                                                import StdEnv
                                                                                s::!Int->Int
                                                                                s _=code {
                                                                                ccall time "I:I"
                                                                                ccall srand "I:I"
                                                                                }
                                                                                r::!Int->Int
                                                                                r _=code {
                                                                                ccall rand "I:I"
                                                                                }
                                                                                $n|s 0<1#k=mape.r e rem n
                                                                                =flatten o mapc.hd[map(i|i==x=c=toChar if(i==y&&x>0)(x+48)(r i rem 26+97))[0..n-1]\x<-k[0..]&y<-k[0..]|x<>y]


                                                                                Try it online!



                                                                                Chooses:




                                                                                • a random x (position of the character in the segment)

                                                                                • a random y that isn't equal to x (position of the digit in the segment)

                                                                                • a random lowercase letter for each position not equal to x and not equal to y unless x is zero







                                                                                share|improve this answer












                                                                                share|improve this answer



                                                                                share|improve this answer










                                                                                answered Dec 3 at 23:42









                                                                                Οurous

                                                                                6,08311032




                                                                                6,08311032






















                                                                                    up vote
                                                                                    0
                                                                                    down vote













                                                                                    JavaScript, 134 bytes





                                                                                    l=>w=>w.replace(/./g,c=>eval("for(s=c;!s[l-1]||s[t?t-1||9:0]!=c;t=s.replace(/\D/g,''))s=(p=Math.random()*36**l,p-p%1).toString(36)"))


                                                                                    Try it online!



                                                                                    This answer chose the encoded string from all possible encoded string uniformly. So it is more possible to make the encoded letter as the first one.






                                                                                    share|improve this answer



























                                                                                      up vote
                                                                                      0
                                                                                      down vote













                                                                                      JavaScript, 134 bytes





                                                                                      l=>w=>w.replace(/./g,c=>eval("for(s=c;!s[l-1]||s[t?t-1||9:0]!=c;t=s.replace(/\D/g,''))s=(p=Math.random()*36**l,p-p%1).toString(36)"))


                                                                                      Try it online!



                                                                                      This answer chose the encoded string from all possible encoded string uniformly. So it is more possible to make the encoded letter as the first one.






                                                                                      share|improve this answer

























                                                                                        up vote
                                                                                        0
                                                                                        down vote










                                                                                        up vote
                                                                                        0
                                                                                        down vote









                                                                                        JavaScript, 134 bytes





                                                                                        l=>w=>w.replace(/./g,c=>eval("for(s=c;!s[l-1]||s[t?t-1||9:0]!=c;t=s.replace(/\D/g,''))s=(p=Math.random()*36**l,p-p%1).toString(36)"))


                                                                                        Try it online!



                                                                                        This answer chose the encoded string from all possible encoded string uniformly. So it is more possible to make the encoded letter as the first one.






                                                                                        share|improve this answer














                                                                                        JavaScript, 134 bytes





                                                                                        l=>w=>w.replace(/./g,c=>eval("for(s=c;!s[l-1]||s[t?t-1||9:0]!=c;t=s.replace(/\D/g,''))s=(p=Math.random()*36**l,p-p%1).toString(36)"))


                                                                                        Try it online!



                                                                                        This answer chose the encoded string from all possible encoded string uniformly. So it is more possible to make the encoded letter as the first one.







                                                                                        share|improve this answer














                                                                                        share|improve this answer



                                                                                        share|improve this answer








                                                                                        edited yesterday

























                                                                                        answered yesterday









                                                                                        tsh

                                                                                        8,13511446




                                                                                        8,13511446






























                                                                                            draft saved

                                                                                            draft discarded




















































                                                                                            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).






                                                                                            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                                            Please pay close attention to the following guidance:


                                                                                            • Please be sure to answer the question. Provide details and share your research!

                                                                                            But avoid



                                                                                            • Asking for help, clarification, or responding to other answers.

                                                                                            • Making statements based on opinion; back them up with references or personal experience.


                                                                                            To learn more, see our tips on writing great answers.




                                                                                            draft saved


                                                                                            draft discarded














                                                                                            StackExchange.ready(
                                                                                            function () {
                                                                                            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodegolf.stackexchange.com%2fquestions%2f176931%2fcomputer-cipher%23new-answer', 'question_page');
                                                                                            }
                                                                                            );

                                                                                            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







                                                                                            Popular posts from this blog

                                                                                            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

                                                                                            Alcedinidae

                                                                                            RAC Tourist Trophy