# Random.Range (464519)

**URL:** https://discussions.unity.com/t/random-range-464519/464519
**Category:** Scripting
**Tags:** Scripting
**Created:** [December 31, 2011, 7:50pm UTC](https://discussions.unity.com/t/random-range-464519/464519 "2011-12-31T19:50:49Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![voltage](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/voltage/32/447970_2.png) [@voltage](https://discussions.unity.com/u/voltage)
#### Post date: [December 31, 2011, 7:50pm UTC](https://discussions.unity.com/t/random-range-464519/464519/1 "2011-12-31T19:50:49Z")

</div>

I’ve been trying to create an OR statement using several variables, such as bools, floats, ints, but the console always neglects it. So I’ve narrowed it down to Setting a Float to a Random.Range, however, although this code runs through the console, there is virtually no effect. Can someone tell me what’s going wrong?

```auto
using UnityEngine;
using System.Collections;

public class RandomCardBack : MonoBehaviour 
{
	public Material first;
	public Material second;
	
	private float turn;
	
	void Awake()
	{
		turn = Random.Range(1, 1.1f);
	}
	
	void Update()
	{
		if (turn == 1)
		{
			gameObject.renderer.material = first;
		}
		
		if (turn == 1.1f)
		{
			gameObject.renderer.material = second;
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![profanicus](https://avatars.discourse-cdn.com/v4/letter/p/d78d45/32.png) [@profanicus](https://discussions.unity.com/u/profanicus)
#### Post date: [December 31, 2011, 8:28pm UTC](https://discussions.unity.com/t/random-range-464519/464519/2 "2011-12-31T20:28:03Z")

</div>

Random.Range(1,1.1f) will return a float between 1 and 1.1 inclusive, so most of the time both your if statements will be false as the result will rarely be exactly those 2 numbers. There are lots of floats between those 2 values!

Use the int version instead, but with the int version the max value is exclusive so you need it to be one higher than the largest value you want, like:  
`Random.Range(0,2)`  
And have your if statement test for 0 or 1

---

<div class="post-metadata">

### Author: ![voltage](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/voltage/32/447970_2.png) [@voltage](https://discussions.unity.com/u/voltage)
#### Post date: [January 1, 2012, 1:35am UTC](https://discussions.unity.com/t/random-range-464519/464519/3 "2012-01-01T01:35:03Z")

</div>

Thanks for explaining that to me! I found your insight very helpful and I’ve gained a bit more knowledge from you. Thanks again.

---

<div class="post-metadata">

### Author: ![npsf3000\_1](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/npsf3000_1/32/466497_2.png) [@npsf3000\_1](https://discussions.unity.com/u/npsf3000_1)
#### Post date: [January 1, 2012, 3:16am UTC](https://discussions.unity.com/t/random-range-464519/464519/4 "2012-01-01T03:16:14Z")

</div>

For future reference ‘Random.value’ returns a float between 0 and 1.

This is useful if one wants a series of fractions - e.g. 30% of the time be red, 30% of the time be blue, 40% of the time be green.

Then you can do something as simple as:

```auto
var val = Random.value;
if (val < 0.3f)
//Red
else if (val<0.6f)
//Blue
else
//Green

```
