4 Replies Latest reply on Jun 27, 2008 8:38 AM by Rothrock

    Generating random number...

      I am using:


      to create a random number 1 to 100... or actually is it 0-100? Sorry, I think it's 0-100. ;-) Anyways... is there any way to make it so it never repeats a number? I'm only 15, and I really don't have any experience with this, so... please bear with me! The only thing I could think of was adding the numbers to an array as they came up. Then if a random number was created that was already in the array, it would create another random number instead. Like I said, I am not really experienced here, so that's probably an absurd solution. There might not even be a way to accomplish this at all!

      What I'm really doing is working on a "Price is Right" game with my friends... just for fun and to work on my Flash/Actionscript skills for use on more useful things. :-) So, to bring up a random product for the user(s) to guess the price of, I was thinking of doing something like this:

      product = Math.round(Math.random()*100);

      if(product = 0) {
      price = 399;
      product0._visible = true;

      if(product = 1) {
      price = 1200;
      product1._visible = true;

      etc. etc.

      That brings me to another question. Is there an elseif in Actionscript? It doesn't seem like it because it doesn't turn blue when I type it. What do I put? Can I just do if if if over and over again?

      But do you see my problem? I don't want people guessing the price of a blender two times in a game if the number 32 comes up twice!

      If you can think of a solution here or a different way to achieve anything I'm trying to do here, let me know!

        • 1. Re: Generating random number...
          clbeech Level 3
          well first of all - kudos! your on the right track, and placing the generated numbers into an array, and then check the array for a match is a pretty good way to do it, good job. but it will take a bit more programming to get it done, i'll demonstrate below. However, i would also consider a system that's slightly different - for instance, you could create an array of data that holds 'products' as Objects with information about the product - and then randomly choose from that array, excluding any that have come up during the game play - similarly by placing them into a 'used' array and checking that. this way you would have more control over things like prices. for instance, it would be strange if a pack of gum cost 99 dollars ;) but you could also build in a system that chooses a 'range' from numbers placed in the data array Object - like the pack of gum cost between 1 and 4 dollars - and then when the product comes up it would generate a number within that range. you could also 'classify' Objects so that they get assigned to games with products of the same class, value range or type - any number of things really, using this type of method - random is good, but for many of the games played you will need a bit more control. just thoughts.

          for your second question - yes you can use an IF/ELSE IF/ELSE syntax and we write them as:

          if( [conditional statement] ) {
          }else if( [conditional statement] ) {

          now to add and check you array for generating a random series, take look at something like the following:
          • 2. Re: Generating random number...
            RaGlitter Level 1
            I agree with clbeech, your well on your way. Rather than create an array of 'used' items I would remove items from the original array of choices. Something like;

            • 3. Re: Generating random number...
              clbeech Level 3
              yeah RaGlitter that will work as well - however, it will also destroy the main array (at least until it's re-instantiated) so if you were to use that method i would make a 'data' array and then use array.slice() to construct a 'run-time' array.

              also FreddyU - i see in reviewing this thread that i had mis-interpreted your request. first yes 0 to 100 ;) second, i see that really you were going to use the generated number with a conditional sequence to 'choose' one of the products rather than create new prices for them. this will work - however there are more efficient ways to do this, mainly the method that I'd spoken of previously. for instance, you can create Objects to store the product data, then place those product Objects within an array - then simply use the number generated to choose one of them from the array, store the number to check against so the product doesn't com up again, or remove the Object from the array as Ra suggests.

              so the array of 'data' Objects would look something like this:
              • 4. Re: Generating random number...
                Rothrock Level 5
                I would create an array of the things I wanted and then shuffle the array. Then I would take the items out of the array one at a time until I had used up the array.