import random


def random_word_swap(sentence, p):
  """Swaps words from a sentence with probability p."""

  def swap_word(new_words):
    idx_1 = random.randint(0, len(new_words) - 1)
    idx_2 = idx_1
    counter = 0

    while idx_2 == idx_1:
      idx_2 = random.randint(0, len(new_words) - 1)
      counter += 1

      if counter > 3:
        return new_words

    new_words[idx_1], new_words[idx_2] = new_words[idx_2], new_words[idx_1]
    return new_words

  words = sentence.split(' ')

  new_words = words.copy()
  n = int(p * len(words))
  for _ in range(n):
    new_words = swap_word(new_words)

  sentence = ' '.join(new_words)

  return sentence