Arrays of random numbers
- Started
- Last post
- 13 Responses
- pkmlta
I'm trying to create an array of random numbers in which each number only appears once. Any suggestions on an easy way to do this?
- Mimio0
1-generate random number
2-put it into an array
3-generate another random number
4- check to see if it's already in your array
5- if not, put in in the next element
6-repeat steps 3-6
- Engage_London0
create a list of numbers in an array 1 to whatever... and then randomly remove one from the array by doing a random seed of the length of the array... and stick it in another array
- ********0
which thread do we post in?
- ********0
First, how many elements in your array, and it can't be infinifty. What are the range of numbers? 0- 100, 0-20?
do a for loop for that ammount of elements.
Generate a random number with random() and check it's value against elements already existing in the array. Do that by getting the array length and having another for loop that checks, if it's not there, append it, if it's not, disregard it.
There you go, should slow down if you want like 1000, but should be reasonably quick for modest amount.
- pkmlta0
respond here. just needed to clarify that i was dealing with actionscript
- pkmlta0
bottlerocket,
i understand what you're up to, but if i loop through my current array of numbers i only know how to check one index at a time. is there a way to check the entire array to see if the new random number already exists?
thanks,
pete
- ********0
you do that with the for loop within a for loop..if that makes sense.
The first on sets up how many numbers you want generate
the second one checks if the number generated matches against any of the numbers already in the array. So it goes through the entire array, one elment at a time and checks it agains the numner you want to add in
- enjine0
yes, the nested loop will work for checking if a number exists, but it will be processor heavy (not o mention you'd be better off, form-wise, using a recursive function to check)
- ********0
indeed..fast at the start, but depending on the number of numbers you want to generate, it will start slowing down
- Engage_London0
my idea would be the same speed throughout
- Engage_London0
layer51 is your friend...
http://proto.layer51.com/d.aspx?…it will also turn you into a lazy git! :)
- ********0
LOL..that's much better, mind, I am not that hot at flash compared to those geeks.
Bet they havn't got a girlfriend though!
;-)
- pkmlta0
thanks all for your help