# \[AS2.0\] math with random nr keeps returning NaN

**URL:** <https://forum.kirupa.com/t/as2-0-math-with-random-nr-keeps-returning-nan/262291>\
**Category:** flash\
**Created:** [June 4, 2008, 5:49pm UTC](https://forum.kirupa.com/t/as2-0-math-with-random-nr-keeps-returning-nan/262291 "2008-06-04T17:49:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lowen\_f](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/lowen_f/32/1656_2.png) [@lowen\_f](https://forum.kirupa.com/u/lowen_f)\
**Post date:** [June 4, 2008, 5:49pm UTC](https://forum.kirupa.com/t/as2-0-math-with-random-nr-keeps-returning-nan/262291/1 "2008-06-04T17:49:52Z")

</div>

Hi,

I have a simple game with some math… It generates a random number (which represents the number of villagers) to start with and then you can use buttons to add different jobs. For example if you start with 20 villages, you can press the farmer button to assign 1 to farmers and decrease 1 of the villagers.

now this is the code for the random number (just took the one out from the help in flash)

```auto

	function randomInwoners(min:Number, max:Number):Number {
    var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
    return randomNum;
	}
	
	var AantalInwoners2:Number
	AantalInwoners2 = randomInwoners(5, 20)
	aantalInwoners.text = AantalInwoners2

```

and this is the simple math I use, add 1 to the ‘boeren’ (farmers) and decrease ‘aantalInwoners2’ with 1…

```auto

var aantalBoeren2:Number = 0;
aantalBoeren.text = aantalBoeren2;

function telop1() {
	aantalBoeren2++;
	aantalBoeren.text = aantalBoeren2;
	aantalInwoners2--
	aantalInwoners.text = aantalInwoners2;
}

b1.onPress = telop1;

```

But everytime I press the button, it gives me the value of NaN. When I don’t use a random number, it does work… I’m not sure what I’m doing wrong.

---

<div class="post-metadata">

**Author:** ![mikkomikko](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@mikkomikko](https://forum.kirupa.com/u/mikkomikko)\
**Post date:** [June 4, 2008, 6:31pm UTC](https://forum.kirupa.com/t/as2-0-math-with-random-nr-keeps-returning-nan/262291/2 "2008-06-04T18:31:54Z")

</div>

it happens because AantalInwoners2 does not equal aantalInwoners2. That capital A messes things up.

```auto
...
var [COLOR="Red"]a[/COLOR]antalInwoners2:Number
...
function telop1() {
	...
	[COLOR="#ff0000"]a[/COLOR]antalInwoners2--
	...
}
...

```

---

<div class="post-metadata">

**Author:** ![lowen\_f](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/lowen_f/32/1656_2.png) [@lowen\_f](https://forum.kirupa.com/u/lowen_f)\
**Post date:** [June 4, 2008, 6:40pm UTC](https://forum.kirupa.com/t/as2-0-math-with-random-nr-keeps-returning-nan/262291/3 "2008-06-04T18:40:12Z")

</div>

daaaamn! can’t believe I made such a stupid mistake, I was looking for all other kind of possibilities in the code itself… oh well 😉 thx for the answer!

---

<div class="post-metadata">

**Author:** ![mikkomikko](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@mikkomikko](https://forum.kirupa.com/u/mikkomikko)\
**Post date:** [June 4, 2008, 7:04pm UTC](https://forum.kirupa.com/t/as2-0-math-with-random-nr-keeps-returning-nan/262291/4 "2008-06-04T19:04:50Z")

</div>

no prob.
