Solved

how to generate random numbers?

Posted on 2000-05-12
18
191 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
  • 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
 
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:bhess1
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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:bhess1
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 45 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
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.

705 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

12 Experts available now in Live!

Get 1:1 Help Now