# Generating A Random Number between 1-20 and keeping a total

**URL:** <https://forum.kirupa.com/t/generating-a-random-number-between-1-20-and-keeping-a-total/183699>\
**Category:** flash\
**Created:** [March 29, 2006, 10:26am UTC](https://forum.kirupa.com/t/generating-a-random-number-between-1-20-and-keeping-a-total/183699 "2006-03-29T10:26:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![tct182](https://avatars.discourse-cdn.com/v4/letter/t/2bfe46/32.png) [@tct182](https://forum.kirupa.com/u/tct182)\
**Post date:** [March 29, 2006, 10:26am UTC](https://forum.kirupa.com/t/generating-a-random-number-between-1-20-and-keeping-a-total/183699/1 "2006-03-29T10:26:18Z")

</div>

I’m trying to make a kids score board where there are 20 icons all with scores behind them between 1 and 20 and a kid chooses an icon, clicks on it to reveal their score, thenthe score is added to their teams running total at the bottom of the window. (There are 2 teams)

The thing i have a problem with is generating the random numbers and also where exactly to drop in the actionscript in the timeline.  
I’ve tried different variations of the Math.Floor(Math.Random) but i cant seem to get it to produce the numbers i want!!!🅰

Unfortunatly i cant upload what i have been working on as i dont have net access on my work pc.

Can anyone help? I’m really confused on this one.

---

<div class="post-metadata">

**Author:** ![kernel\_flash](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@kernel\_flash](https://forum.kirupa.com/u/kernel_flash)\
**Post date:** [March 29, 2006, 10:35am UTC](https://forum.kirupa.com/t/generating-a-random-number-between-1-20-and-keeping-a-total/183699/2 "2006-03-29T10:35:43Z")

</div>

A simple formula for generating number between a and b.

ActionScript Code:  
[AS]

[FONT=Courier New][LEFT]  
function randomBetweenCOLOR=#000000[/COLOR]  
[COLOR=#000000]{[/COLOR]  
return (a+[COLOR=#0000ff]Math[/COLOR].[COLOR=#0000ff]floor[/COLOR]COLOR=#000000;[/COLOR]  
[COLOR=#000000]}[/COLOR]  
[/AS]

[/LEFT]

[/FONT]

use the function in a for loop assigning them to the icons.

---

<div class="post-metadata">

**Author:** ![kernel\_flash](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@kernel\_flash](https://forum.kirupa.com/u/kernel_flash)\
**Post date:** [March 29, 2006, 10:38am UTC](https://forum.kirupa.com/t/generating-a-random-number-between-1-20-and-keeping-a-total/183699/3 "2006-03-29T10:38:49Z")

</div>

i’m sorry but i messed up with the code while editing.

this is the simple function  
[AS]  
function randomBetween(a,b)  
{  
return (a+Math.floor(Math.random()\*(b+1)));  
}

---

<div class="post-metadata">

**Author:** ![tct182](https://avatars.discourse-cdn.com/v4/letter/t/2bfe46/32.png) [@tct182](https://forum.kirupa.com/u/tct182)\
**Post date:** [March 29, 2006, 10:47am UTC](https://forum.kirupa.com/t/generating-a-random-number-between-1-20-and-keeping-a-total/183699/4 "2006-03-29T10:47:57Z")

</div>

Thanks for the speedy response! I will try it out as soon as I get back.(-:

I dont suppose you would know how i could get it to keep 2 seperate totals at the bottom of the screen do you? 1 total each for Team A and Team B - they would take it in turns to reveal a score and that score would be added to the total for their team.

Cheers.

---

<div class="post-metadata">

**Author:** ![Dhiraj](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@Dhiraj](https://forum.kirupa.com/u/Dhiraj)\
**Post date:** [March 29, 2006, 3:03pm UTC](https://forum.kirupa.com/t/generating-a-random-number-between-1-20-and-keeping-a-total/183699/5 "2006-03-29T15:03:51Z")

</div>

Hi,  
a bit confused about your heading for the problem, but then  
this what i have for you , if i’m correct.

This function returns a random number, with given limit.

//----------------------------------------------------------------  
// getRangeInclusive  
//  
// Returns a random number secified within the range including extrem values  
// Usage : var myVar = getRangeInclusive(lowerLimit, higherLimit) ;  
//  
function getRangeInclusive(iMin, iMax) {  
var iDiff = iMax-iMin+1;  
var v1 = Math.floor(Math.random()\*iDiff)+iMin;  
return (v1);  
}  
//----------------------------------------------------------------

Dhiraj

---

<div class="post-metadata">

**Author:** ![Dhiraj](https://avatars.discourse-cdn.com/v4/letter/d/e47c2d/32.png) [@Dhiraj](https://forum.kirupa.com/u/Dhiraj)\
**Post date:** [March 29, 2006, 3:12pm UTC](https://forum.kirupa.com/t/generating-a-random-number-between-1-20-and-keeping-a-total/183699/6 "2006-03-29T15:12:55Z")

</div>

function randomBetween(a, b) {  
return (a+Math.floor(Math.random()\*(b+1)));  
}

var iN = randomBetween(1, 20);

with 1 and 20 as argument , the return value some time was 21.

possible

my earlier function works fine

function getRangeInclusive(iMin, iMax) {  
var iDiff = iMax-iMin+1;  
var v1 = Math.floor(Math.random()\*iDiff)+iMin;  
return (v1);  
}

Dhiraj

---

<div class="post-metadata">

**Author:** ![kernel\_flash](https://avatars.discourse-cdn.com/v4/letter/k/a87d85/32.png) [@kernel\_flash](https://forum.kirupa.com/u/kernel_flash)\
**Post date:** [March 30, 2006, 4:46am UTC](https://forum.kirupa.com/t/generating-a-random-number-between-1-20-and-keeping-a-total/183699/7 "2006-03-30T04:46:52Z")

</div>

:sure:

---

<div class="post-metadata">

**Author:** ![tct182](https://avatars.discourse-cdn.com/v4/letter/t/2bfe46/32.png) [@tct182](https://forum.kirupa.com/u/tct182)\
**Post date:** [April 5, 2006, 9:55am UTC](https://forum.kirupa.com/t/generating-a-random-number-between-1-20-and-keeping-a-total/183699/8 "2006-04-05T09:55:06Z")

</div>

Thanks for the response. I can’t get these scripts to work. I’m attaching them to the point in the timeline but i just seem to be not doing something right. When i publish the file to test it it usually comes up with something along the lines of “level2\_” instead of the random number between 1 and 20.

Any ideas?

Peter  
(Sorry i’m unable to upload the file - if someone would be kind enough to post a sample for me to look at i would be ever greatfull - Thanks!)😉
