Skip to content

Instantly share code, notes, and snippets.

@stober
Created February 29, 2012 18:46
Show Gist options
  • Save stober/1943451 to your computer and use it in GitHub Desktop.
Save stober/1943451 to your computer and use it in GitHub Desktop.
Random Argmax in Python
#!/usr/bin/env python
"""
Author: Jeremy M. Stober
Program: RARGMAX.PY
Date: Wednesday, February 29 2012
Description: Simple rargmax function.
"""
import numpy as np
import random as pr
def rargmax(vector):
""" Argmax that chooses randomly among eligible maximum indices. """
m = np.amax(vector)
indices = np.nonzero(vector == m)[0]
return pr.choice(indices)
if __name__ == '__main__':
test = [0,1,2,2]
for i in range(10):
print rargmax(test)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment