Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how to generate random numbers?

Posted on 2000-05-12
18
Medium Priority
?
199 Views
Last Modified: 2010-04-03
please could someone tell me how could I generate more than 10 DIFFERENT random numbers in flash please (using scripting....)
0
Comment
Question by:tine
[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
  • 7
  • 7
  • 2
  • +1
18 Comments
 
LVL 4

Expert Comment

by:RoyKruyshaar
ID: 2803682
You want them in an array or just 10 times a random number?
0
 
LVL 4

Expert Comment

by:RoyKruyshaar
ID: 2803698
Well here is a script:

Set Variable: "looper" = "10"
Loop While (looper > 0)
 Set Variable: "var"&looper = Random ( 100 )
 Set Variable: "looper" = looper - 1
End Loop

where "looper" is the variable that saids the you want, and the number in the RandomStatement saids that the random number must be between 0 and 99.

When you have further questions, feel free to ask.

Good Luck,

Roy
0
 

Author Comment

by:tine
ID: 2805230
sorry........ but those random numbers generated are not unique..... two idential numbers occured..... please tell how to generate more than 10 unique random numbers (best to be within 10-20) at the same time..... thanks a lot
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Expert Comment

by:SuperMario
ID: 2807416
This is done with nested loops.

I am working on a .fla for you that accomplishes what you need. You can simply use the action script anywhere else you desire.

I'll  have it for you soon.
-Dan
0
 
LVL 3

Expert Comment

by:SuperMario
ID: 2807740
This script would work if it were possible to break out of a loop with some command. I'll show you in the code where the loop needs to be broken.

On (Release)
      Set Variable: "numstr" = ""
      Set Variable: "i" = 0
      Loop While (i < 10)
            Set Variable: "Found" = "False"
            Set Variable: "x" = Random (20)
            Set Variable: "y" = 0
            If (i)
                  Loop While ((y < i) and Found eq "False")
                        If (Eval("var" & i) eq x)
                              Set Variable: "Found" = "True"
                        End If
                        Set Variable: "y" = y + 1
                  End Loop
            End If
            If (Found eq "False")
                  Set Variable: "var" & i = x
*Break out of the loop here!*
            End If
            Set Variable: "numstr" = numstr & " " & Eval("var" & i)
            Set Variable: "i" = i + 1
      End Loop
End On
0
 

Author Comment

by:tine
ID: 2811474
Really cool........ do u think I could make it to generate 20 random numbers rather than 10 only...... I tried to change
Loop While (i < 10)
to
Loop While (i < 20)

but then the script start to go pear shape..... they are not unique anymore.... plese help
0
 
LVL 3

Expert Comment

by:SuperMario
ID: 2812192
Sorry, change the

Random(20)

to

Random(10) + 1
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2812396
This is a modification of the code that SuperMario posted.  It will not generate duplicates.

NOTE:  If you change the LOOP WHILE number, you need to make sure that the RANDOM statement range is at least as great.  In the code below, it loops 20 times, and the Random statement can generate 20 different numbers.  If the LOOP count increases, the RANDOM # needs to increase as well.

On (Release)
      Set Variable: "numstr" = "" 
      Set Variable: "i" = 0
      Loop While (i < 20)
            Set Variable: "Found" = "False"
            Set Variable: "x" = Random (20)+1
            Set Variable: "y" = 0
            If (i)
                  Loop While ((y < i) and Found eq "False")
                        If (Eval("var" & i) eq x)
                              Set Variable: "Found" = "True"
                        End If
                        Set Variable: "y" = y + 1
                  End Loop
            End If
            If (Found eq "False")
                  Set Variable: "var" & i = x
                  Set Variable: "i" = i + 1
                  Set Variable: "numstr" = numstr & " " & Eval("var" & i)
            End If
      End Loop
End On
0
 
LVL 3

Expert Comment

by:SuperMario
ID: 2812606
Yeah, You have to use the numbers that bhess1 posted because I thought you wanted 10 numbers, I didn't read your post correctly

but have the loop variable at 20 and Random(20) + 1.

-Dan
0
 

Author Comment

by:tine
ID: 2813516
i have followed all the steps u guys told me and I still couldn't generate 20 unique numbers!! Some of them are still the same..... please help.... I need this egently!!

Do u think u could put the script in flash just to make sure I won't make any silly mistakes, please. thanks a lot
0
 
LVL 32

Expert Comment

by:Brendt Hess
ID: 2814192
Hmmm.... I'm surprised.  If you are still getting duplicates, then something basic must be wrong with the code that I am missing.

Can you post the value contained in numstr after running the code?  Also, just to be sure, can you post the code you actually ran (so we can look for possible typos).
0
 

Author Comment

by:tine
ID: 2820673
This is what I have

On (Release)
      Set Variable: "numstr" = ""
      Set Variable: "i" = 0
      Loop While (i < 20)
            Set Variable: "Found"= "False"
            Set Variable: "x" = Random (20) + 1
            Set Variable: "y" = 0
            If (i)
                  Loop While ((y < i) and Found eq "False")
                        If (Eval("var" & i) eq x)
                              Set Variable: "Found" = "True"
                        End If
                        Set Variable: "y" = y + 1
                  End Loop
            End If
            If (Found eq "False")
                  Set Variable: "var" & i = x
                  Set Variable: "i" = i + 1
                  Set Variable: "numstr" = numstr & " " & Eval("var" & i)  
            End If
      End Loop
End On

This is what I have entered....... and I put these scipt into the action of a button and I see all the number generated by opening the list variable window.....
0
 
LVL 3

Expert Comment

by:SuperMario
ID: 2820856
Yes, you can see them in a text box by creating a new one, making sure it's an editable text box (the ab| button must be down on the drawing toolbar) and right clicking it. Go to properties and for Variable Name type in numstr.
I hope this helps,
-Dan
0
 

Author Comment

by:tine
ID: 2821712
still can't do it!!! I don't think I could make it...... have u tried to make it work before? cos it still generate same numbers man,..... please help!!!!
0
 
LVL 3

Accepted Solution

by:
SuperMario earned 135 total points
ID: 2823244
I have the C++ code I wrote for it but it's very difficult to adapt to ActionScript.

Would you like me to post that?

-Dan
0
 

Author Comment

by:tine
ID: 2826096
thanks for all the help but I think it is ok....... I think u can take the points
0
 

Author Comment

by:tine
ID: 2826100
thanks a lot guys I think u deserve the points
0
 
LVL 3

Expert Comment

by:SuperMario
ID: 2827363
I'm glad I could help.

-Dan
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
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 select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.
The goal of the tutorial is to teach the user what frame rate is, how to control it and what effect it has on the video.

670 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