# Need an answer quick: Random()

**URL:** <https://forum.kirupa.com/t/need-an-answer-quick-random/5402>\
**Category:** Uncategorized\
**Created:** [September 10, 2002, 10:12am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402 "2002-09-10T10:12:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![FLATLINE\_\_](https://avatars.discourse-cdn.com/v4/letter/f/f07891/32.png) [@FLATLINE\_\_](https://forum.kirupa.com/u/FLATLINE__)\
**Post date:** [September 10, 2002, 10:12am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/1 "2002-09-10T10:12:23Z")

</div>

This should be a really easy question. Can random() select randomly between letters or strings instead of numbers?  
Like this maybe:

milk = random(cookie,box,spoon);

Is something like this possible?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 10, 2002, 10:49am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/2 "2002-09-10T10:49:19Z")

</div>

No, I don’t think it does. But please someone correct me if I’m wrong.

You would however be able to generate a random number, then use that to pull a string out of an array, for example.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 10, 2002, 12:11pm UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/3 "2002-09-10T12:11:46Z")

</div>

yeah… I would do that with a simple array situation… that’s what they are for

storage=New array(cookie,box,spoon);

something=storage[Math.floor(Math.random()\*3+1)];

or something like that anyway

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 10, 2002, 12:15pm UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/4 "2002-09-10T12:15:45Z")

</div>

Can you please explain this line?

something=storage[Math.floor(Math.random()\*3+1)];

Thanks.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 10, 2002, 12:25pm UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/5 "2002-09-10T12:25:52Z")

</div>

‘something’ is a variable that I am setting to equal one of the array items. It could be anything including a dynamicaly created variable… though that seems redundent with an array already set up.

The line “storage[Math.floor(Math.random()\*3+1)];” is supposed to do this… but I haven’t tested it… it’s just from my head

storage[ this is the array name as set up in the first line. What belongs inside it’s square brackets is a number. (I already see a mistake… I’ll fix it below) But we can be tricky and place a variable or an expression in the square brackets instead, as long as it translates to a valid number.

Math.floor(Math.random()\*3+1) this is supposed to be generating a number between 1 and 3, but I forgot that an array’s first item is number 0, not number 1. It’s a mistake I make all the time. So the a/s should really be

storage[Math.floor(Math.random()\*3)];

Math.floor(); rounds whatever is inside it’s brackets to the next lowest whole number.  
Math.random() produces a number between 0.00 and 1.00. Multiply that by three and you get a number between 0.00 and 2.00 (3\*.99=2.97) rounded down at least… it will always be 0,1,or 2.

] then finaly a closing square bracket

These are the fun commands where you start getting wild ideas about dynamic creation and sorting of information. Be prepared… you will soon be insane like the rest of us. 😉

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 10, 2002, 2:46pm UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/6 "2002-09-10T14:46:36Z")

</div>

I don’t get it why can’t you just do this: Math.random(2) ?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 10, 2002, 4:14pm UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/7 "2002-09-10T16:14:28Z")

</div>

Math.random(3) = Messy decimal numbers

Math.floor(Math.random()\*3) = Nice whole numbers that are rounded down (rounded down so you can get zero)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 2:44am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/8 "2002-09-11T02:44:27Z")

</div>

Math.random(3); would be improper syntax. The old style was  
random(3); but Math.random(3) wont do what you’re thinkin.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 2:46am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/9 "2002-09-11T02:46:48Z")

</div>

Haha, good point, it does work though david. That is what I have been meaning to ask you but forgot.

I know it is written as Math.random()\*3 (and that is how I always write it), but why is Math.random(3) considered improper syntax when it works perfectly fine?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 2:49am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/10 "2002-09-11T02:49:22Z")

</div>

hmm… I don’t know. I’ve just always believed that it wouldn’t work. Perhaphs I just haven’t experiemented with it.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 2:52am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/11 "2002-09-11T02:52:52Z")

</div>

Ah yes, after re-experimenting with it while using the trace function, I found it only returns 1’s and 0’s if you have it has Math.random(3)… makes me wonder what I did different when I first tested it…eh oh well, I still know I do it right now.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 3:01am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/12 "2002-09-11T03:01:27Z")

</div>

using Math.random(3); I came up with these number out of ten tries

0.182616225528818  
0.246697391498227  
0.496306900166118  
0.294558602056726  
0.0384465507410683  
0.804301160762227  
0.274645778944085  
0.671068015820844  
0.376926560130402  
0.70065694567778

using Math.random()\*3; I came up with these

1.44232590051476  
2.42299682899518  
0.169483414930051  
0.996716877909711  
1.47894309949081  
1.74336946464301  
1.63587004488142  
2.7064569735464  
0.101575112948928  
1.76893623628138

So the Math.random(3); is not working. Not the way random is supposed to work anyway.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 3:02am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/13 "2002-09-11T03:02:25Z")

</div>

you probebly did

random(3);

the first time. That is depricated, but it still works, and will do a random 1 through 3

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 3:03am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/14 "2002-09-11T03:03:42Z")

</div>

I just used Math.round when I did it because all of those decimals get on my nerves…🙂 I got…

1  
1  
1  
0  
0  
1  
0  
1

You get the point.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 3:04am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/15 "2002-09-11T03:04:57Z")

</div>

I Probably did. It was back when I tried AS for the very first time.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 6:34am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/16 "2002-09-11T06:34:14Z")

</div>

Thanks for all the help guys! (and gals 😉 )

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 6:35am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/17 "2002-09-11T06:35:39Z")

</div>

Just a little something:

```auto
storage=New array("cookie","box","spoon");
something=storage[Math.floor(Math.random()*storage.length-1)];

```

would be better if the length of storage changes.

pom :asian:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 6:43am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/18 "2002-09-11T06:43:06Z")

</div>

Smart Pom… always thinking.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 6:46am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/19 "2002-09-11T06:46:32Z")

</div>

It’s Pom, who would expect less:)

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [September 11, 2002, 7:20am UTC](https://forum.kirupa.com/t/need-an-answer-quick-random/5402/20 "2002-09-11T07:20:13Z")

</div>

:blushing:

And Lost, I’ve just noticed you have more posts than Upu himself! Now correct me if I’m wrong, Upu, but I don’t think this has ever happened to you, has it? Even on the old forums?

pom 😛

[Next page](https://forum.kirupa.com/t/need-an-answer-quick-random/5402.md?page=2)
