Solved

get random numbers from multiple strings

Posted on 2009-05-17
1
268 Views
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

0
Comment
Question by:jasotasticon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 4

Accepted Solution

by:
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
A
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

0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting CFGRID from Flash to HTML in Coldfusion 1 105
Extracting Icon but saves ico as blank 9 113
SVG, jquery, menu 14 73
Pinnacle Studio 19 5 60
Here are some practices and techniques that can be adopted into your Flash/Flex application development process. Note: Not all "performance tips" provide an immediately-recognizable benefit.   This article does not include timing validation data,…
I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
Learn how to download your full Prezi presentation for offline presenting. Prezi doesn’t have to be viewed and shared in a web browser, even with a free account you can download your full presentation to share with others. Be sure to download any vi…

726 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