Skip to content

Instantly share code, notes, and snippets.

@smspillaz
Created October 31, 2018 17:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smspillaz/ca9abed4bcf0d4f0dd4eb39c0d610c2d to your computer and use it in GitHub Desktop.
Save smspillaz/ca9abed4bcf0d4f0dd4eb39c0d610c2d to your computer and use it in GitHub Desktop.
Random Search in Pyton
import random
def random_search(array, iterations, objective, *args):
"""Do a random search over the array for :iterations:."""
best = array[0]
best_value = objective(best, *args)
for i in range(iterations):
candidate = random.sample(best, len(best))
value = objective(candidate, *args)
if value < best_value:
best = candidate
best_value = value
return best, best_value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment