Coding help
Author |
Message |
odion29
|
Posted: Fri May 29, 2009 8:52 am Post subject: Coding help |
|
|
i'm trying to get the program to randomly post 50 numbers from 1 to 100 without repeats.
What would you do to avoid repeats in the coding?
for i : 1 .. 50
randint (number, 1, 100)
put number
end for
Any help would be appreciated.
Thanks |
|
|
|
|
|
Sponsor Sponsor
|
|
|
jbking
|
Posted: Fri May 29, 2009 9:24 am Post subject: Re: Coding help |
|
|
Well, let's think about this for a moment. How would you know that the nth random number is a repeat? There would have to be something knowing what was already picked, right? What could hold these results as even for the second number you'd have to check it against the first number, right?
Does that help you at all? |
|
|
|
|
|
Dusk Eagle
|
Posted: Fri May 29, 2009 2:34 pm Post subject: Re: Coding help |
|
|
If I understand jbking's way correctly, he's telling you to store values already chosen in an array, and then check if the next random number is already located within the array of chosen numbers. This works when there is a large range of numbers to choose from and only a few numbers need to be picked, but think of it this way - on the 98th time picking a random number between 1 and 100, what are the chances that you will pick a unique number the first time through? second? third?
A better way to do it in the above case is to have an array of all your unchosen numbers. When a number is chosen, remove it from the array, resize the array, and then pick another random number within the range of lower(array) and upper(array). This ensures that your program will only have to generate a random number once each and every time through a loop. |
|
|
|
|
|
BigBear
|
Posted: Fri May 29, 2009 2:44 pm Post subject: RE:Coding help |
|
|
If the program runs for less than a second then the numbers will always be different
This is because turing generates that random number off a algorithm that depends on the current second and it will use a slightly different one if there is two calls in a second
This is because a random number is impossible
Not really what you were looking for but anyways |
|
|
|
|
|
Tony
|
Posted: Fri May 29, 2009 3:37 pm Post subject: RE:Coding help |
|
|
@BigBear -- you are talking about something completely different. Dusk Eagle has got the right idea. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Dusk Eagle
|
Posted: Fri May 29, 2009 10:15 pm Post subject: Re: Coding help |
|
|
Reading through my post again, I see I did not phrase my final sentence in the first paragraph very clearly, so I'll try again in case anyone's confused:
You are randomly arranging the numbers 1 to 100 in an array. No number may be in the array more than once. When finding the 98th element of the array, you generate a random number. You then check to see if that number exists in the array. It probably does, so you'll probably have to try again. Trying again, your new random number is probably also in the array already, so you'll have to try yet again. This goes on an unpredictable number of times. That is why it is a better idea to do it the way I mentioned in my second paragraph above. |
|
|
|
|
|
|
|