Solved

# how to generate random numbers?

Posted on 2000-05-12
197 Views
please could someone tell me how could I generate more than 10 DIFFERENT random numbers in flash please (using scripting....)
0
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
• 7
• 7
• 2
• +1

LVL 4

Expert Comment

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

LVL 4

Expert Comment

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

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

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

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

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

ID: 2812192
Sorry, change the

Random(20)

to

Random(10) + 1
0

LVL 32

Expert Comment

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

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

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

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

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

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

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

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

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

Author Comment

ID: 2826100
thanks a lot guys I think u deserve the points
0

LVL 3

Expert Comment

ID: 2827363

-Dan
0

## Featured Post

Question has a verified solution.

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