# Thread: extracting item from an array

#### Hybrid View

1. Registered User
Join Date
Nov 2005
Location
Bundanoon: first village to ban bottled water
Posts
230

## extracting item from an array

This randomising function is like a coin toss, it gives a numerical result and displays it:

function Ran() {
var c = Math.random();
return (c < 0.5) ? [0, show0()] : 1, show1()];
}

I want to extract the numerical outcome, 0 or 1, from this and from two other similar functions, sum them, and then run more functions based on the possible sums.

What is the syntax for getting to the numbers? something like

var n = Ran().[0] ?

2. Registered User
Join Date
May 2011
Posts
194

## maybe this

Hi,

function Ran()
{
var arReturn=new Array
(
1,
show1()
);

if(Math.random()< 0.5)
{
arReturn[0]--;
arReturn[1]=show0();
}

return arReturn;
}

----------------------------

var n = Ran().[0] --> Ran() must return a pointer of an object with property array
but Ran() is a pointer to code (return pointer of array).

3. Registered User+
Join Date
Feb 2006
Posts
2,930
You can simplify things by rounding the random number to return 0 or 1.

Code:
```function flip() {
return Math.round(Math.random())
}```

4. Registered User
Join Date
Nov 2005
Location
Bundanoon: first village to ban bottled water
Posts
230
thanks to both

getting the hang of it...

##### Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•