-5

I'm making an AS3 program, and in it, when a button (instance name "buy") is clicked, 5 names are randomly picked from a list of about 120 names.

Simple example:

Bob
George
Tom
Mohammed
Adam
Moses
Aaron
David

From these 8 names, it would, for example, randomly pick 3 names.

I also need to make it so that some names are picked more frequently than others. For example, Mohammed will be picked 50% of the time, David 20% of the time, Bob 2% of the time...

How do I do this? I'm pretty new to AS3, and I only know how to do simple things so far.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
MNOPYZ
  • 55
  • 1
  • 2
  • 13
  • 2
    This is not an AS3 issue, because you are asking the algorithm to solve a generic problem. – SharpEdge Jun 09 '14 at 13:54
  • I'm asking; Can you solve this "generic problem" with AS3, and how do you do it? – MNOPYZ Jun 09 '14 at 14:49
  • 1
    I believe SharpEdge is correct, this question is off topic because it does not "demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work[...]" [Please see this link](http://stackoverflow.com/help/on-topic) for more information about what is considered on topic – CyanAngel Jun 09 '14 at 15:22
  • 1
    @CyanAngel - I agree! It's not a hard problem, but I don't find any idea in spending 20 minutes writing the whole code by myself, so that the user can get it just like that. This leads to wrong attitude of asking questions for every obstacle you've got, and from programming, this becomes whining someone to do your job. If at least there was some effort I wouldn't mind helping! But this way is pretty wrong! – Andrey Popov Jun 10 '14 at 08:44
  • I guess you're right, I didn't put enough effort into solving this problem myself, at first I was hoping to get a general idea of how to solve the problem while I was working on another problem altogether. It's clear to me now that it wasn't multitasking, it was just plain laziness :P – MNOPYZ Jun 10 '14 at 11:50
  • Now you can easily close your question, do some efforts and if you still need help - show some code and we will help. – Andrey Popov Jun 10 '14 at 12:00

2 Answers2

1

visit this link to find your solution.

or try this code

var originalArray:Array = new Array('Bob', 'George', 'Tom', 'Mohammed', 'Adam', 'Moses', 'Aaron', 'David');
var shuffledArray:Array = originalArray.sort(shuffle);
trace(shuffledArray);


private function shuffle(originalArray,shuffledArray):int
{
    var sortNum : int = Math.round(Math.random() * 2) - 1;
    return sortNum;
}   
Community
  • 1
  • 1
subrat71
  • 308
  • 3
  • 15
  • i have found this link hope its usefull to you... [http://stackoverflow.com/questions/9330394/how-to-pick-an-item-by-its-probability] – subrat71 Jun 10 '14 at 09:12
  • Thanks, this was my problem in the first place; As I'm new to AS3, I didn't know much about Arrays, I just jumped to the complicated stuff. Thanks for your help, but I still need to add possibilities. I know this can be done by adding an element multiple times, but to do that I'd need to make up to 3000 elements for each pack, which would slow the program down terribly. Is there any other option? Thank you. – MNOPYZ Jun 13 '14 at 16:12
  • use vector instead of array – subrat71 Jun 14 '14 at 02:53
  • Hmm? How would it help if I used a Vector instead? According to what I've read about it so far, it's just a more restricted kind of Array... – MNOPYZ Jun 14 '14 at 11:43
  • @subrat71 : Your code worked perfectly, but I couldn't even slightly understand the "theory" behind it... When I try " var sortNum : int = Math.round(Math.random() * 2) - 1" alone, it gives me either 1, 0, or -1. I understand how that works, but how does it shuffle using that? Also, in the line shuffle(originalArray,shuffledArray) , Why is it that you didn't simply do shuffle(); ? Sorry for asking so much, it's just that I'm rather new to AS3. – MNOPYZ Jun 14 '14 at 12:53
  • if you like my code then mark it as answer. yes you can use the function without parameter. Don't ask this questions just try yourself with parameter and without parameter. If no difference came then you can delete the parameters. – subrat71 Jun 14 '14 at 16:31
  • @subrat71 I know you can't do it with parameters, but I would like to understand why. The function doesn't seem to include those parameters in it, so why doesn't it work when they're not included? I would also like to understand the logic behind using " var sortNum : int = Math.round(Math.random() * 2) - 1". I'm very sorry for being such a burden, I'm new to AS3, and I would like to understand what I'm writing. – MNOPYZ Jun 15 '14 at 10:19
  • 1
    Follow this documentation "http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#sort()" Hope you understand – subrat71 Jun 16 '14 at 05:30
  • Alright, thanks very much for all your help, sorry for being such a noob ^^ – MNOPYZ Jun 16 '14 at 11:23
0

Not writing any code now, ( show me yours i'll show you mine)
but I'll give you an Idea of how this can be done.

  • Make a Dictionary with the names and their weights.
  • Create an Array of Strings, with all the names occuring as often as their weight.
  • Shuffle the Array
  • Pick desired amout of random (unique) names
M4tchB0X3r
  • 1,531
  • 1
  • 15
  • 28