unity object not rotate with camera code example

Example 1: how to randomly rotate rigidbody of object in unity

public float speed; private Rigidbody2D rigidbody2d;  void Start() {     rigidbody2d = GetComponent<Rigidbody2D>();     rigidbody2d.angularVelocity = Random.value * speed; }

Example 2: unity prevent object from leaving camera view

using UnityEngine; 
using System.Collections; 
public class Example : MonoBehaviour {
  void Update() {         
    Vector3 pos = Camera.main.WorldToViewportPoint (transform.position);         
    pos.x = Mathf.Clamp01(pos.x);         
    pos.y = Mathf.Clamp01(pos.y);         
    transform.position = Camera.main.ViewportToWorldPoint(pos);     
  } 
}