Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 201
  • Last Modified:

how to generate random numbers?

please could someone tell me how could I generate more than 10 DIFFERENT random numbers in flash please (using scripting....)
0
tine
Asked:
tine
  • 7
  • 7
  • 2
  • +1
1 Solution
 
RoyKruyshaarCommented:
You want them in an array or just 10 times a random number?
0
 
RoyKruyshaarCommented:
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
 
tineAuthor Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SuperMarioCommented:
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
 
SuperMarioCommented:
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
 
tineAuthor Commented:
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
 
SuperMarioCommented:
Sorry, change the

Random(20)

to

Random(10) + 1
0
 
Brendt HessSenior DBACommented:
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
 
SuperMarioCommented:
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
 
tineAuthor Commented:
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
 
Brendt HessSenior DBACommented:
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
 
tineAuthor Commented:
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
 
SuperMarioCommented:
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
 
tineAuthor Commented:
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
 
SuperMarioCommented:
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
 
tineAuthor Commented:
thanks for all the help but I think it is ok....... I think u can take the points
0
 
tineAuthor Commented:
thanks a lot guys I think u deserve the points
0
 
SuperMarioCommented:
I'm glad I could help.

-Dan
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 7
  • 7
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now