Widgets don't have a renderer, so your renderer.material won't work. If you have a UITexture, you need to get a component of type UITexture, then change its 'mainTexture' as r.pedra mentioned. If you aren't sure how to get components in Javascript, I can only suggest going back to the very basics of Unity. I don't use Javascript myself, so I can't help you here. C# equivalent of what you're doing is:
using UnityEngine;
[RequireComponent
(typeof(UITexture
))] public class Test : MonoBehaviour
{
public Texture2D textures;
void Start ()
{
GetComponent<UITexture>().mainTexture = textures[Random.Range(0, 31)];
}
}
If the "sizing is off" that's because changing the texture won't make it resize. You need to call MakePixelPerfect() to make it change its dimensions after changing the texture.
UITexture tex = GetComponent<UITexture>();
tex.MakePixelPerfect();