import random
import rhinoscriptsyntax as rs

def RandomMaterialColor():
    """ assign a random material color to selected objects"""
    
    f = 1 + 2 + 4 + 8 + 16 + 32 + 256 + 512 + 4096 + 8192 + 1073741824
    obj_ids = rs.GetObjects("Select objects to color material", f, True, True, True)
    if not obj_ids: return
    
    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)
    
    rs.EnableRedraw(False)
    
    for obj_id in obj_ids:
        index = rs.ObjectMaterialIndex(obj_id)
        if index == -1:
            index = rs.AddMaterialToObject(obj_id)
        if index>-1:
            rs.MaterialColor(index, (r,g,b))
            # remove next 2 lines to create unnamed materials
            mat_name = "RGB {},{},{}".format(r,g,b)
            rs.MaterialName( index, mat_name )
    
    rs.EnableRedraw(True)

RandomMaterialColor()