get random numbers from multiple strings

Posted on 2009-05-17
Last Modified: 2013-11-11
I have three strings, and I want to pick a random item from one of the strings and return three variables: 1 the name of the string it is in 2 the name of the item in the string, and 3 the number of the item in the string. However, I need each item to be just as likely to be picked each time, even if one string has only one item in it and another has 50 items.

My current method causes the shortest strings items to occur more often because I am picking which string first etc.

I am working on a solution, but it seems very convoluted and overly long, perhaps I am missing something obvious.

please help,
- J

function determineImageToLoad() {

	// picks a random list

	imageListToLoadFromNumber = Math.floor(Math.random()*folderListArray.length);

	trace("imageListToLoadFromNumber = "+imageListToLoadFromNumber);


	// uses the number to pick which list to draw from

	imageListToLoadFrom = _root[_root.folderListArray[imageListToLoadFromNumber]+"ArrayHolder"];

	trace("imageListToLoadFrom = "+imageListToLoadFrom);


	// pics a specific number from that list

	imageToLoadNumber = Math.floor(Math.random()*imageListToLoadFrom.length);

	trace("imageToLoadNumber = "+imageToLoadNumber);


	// pics the name from that list that goes with the number

	imageToLoad = imageListToLoadFrom[imageToLoadNumber];

	trace("imageToLoad = "+imageToLoad);

	//trace("loading "+imageToLoad);


Open in new window

Question by:jasotasticon
1 Comment

Accepted Solution

Adam Chan earned 500 total points
ID: 24410581
Hi jasotasticon,

Have you considered putting all the items in one array and random pick from there, and then identify which array it originated from afterwards?

See attached pseudo code

hope it helps
list1 {a, b}

list2 {c, d, e, f}

list3 {g, h, i, j, k, l}

lists {list, list2, list3}

master_ list = {a, b, d, e, f, g, h, i, j, k, l}    // do your random picking with this list

// say the item you picked is 'g'

// then you can do a simple loop with an array.indexOf(g) check to see 

// which of the original 3 list it came from, and pull your other 2 variables out that way.

for each (list in lists)

  if (list.indexOf(g) != -1)    // if 'g' is found in this list

    variable1 = list

    variable2 = g

    variable3 = list.length

Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
camtasia alternative that can record 3 hours video plus audio 24 105
SVG, D3, scaling, translation 11 142
Lite dgn, dwg free viewer 1 99
Slow Printing 8 71
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
In my long career of working as an actionscript developer, I had spent sleepless night often working hard to solve some small problems which actually took a lot of my development time; later found out the solutions to be a line or two. Here are s…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.
Learn how to create interesting presentations by including videos to keep your audience engaged using Prezi. Select "Insert" from the top menu in your Prezi editor: Select "YouTube Video": Paste the video URL into the prompt: "Select "Insert":…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now