dkl
Full Member
Posts: 234
|
Post by dkl on Nov 29, 2021 20:44:44 GMT -5
I have search the web for this but could not find any answers. Lots of explanations on how to generate numbers from 1 to 10 or 20 to 50, but nothing relative to my question below.
How can I generate a random number between 1 - 10, BUT...... only get 1 instance of each number? i.e. 2,4,6,1,3,8,7,10,5,9.
I suppose that one can keep generating random numbers and store them in an array() until one has found them all
Any easier/quicker way?
OR
Could I have an array of numbers e.g. radNum$ = "1 2 3 4 5 6 7 8 9 10" and then jumble them up?
|
|
|
Post by tsh73 on Nov 29, 2021 21:22:19 GMT -5
|
|
|
Post by Brandon Parker on Nov 29, 2021 21:59:39 GMT -5
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Nov 30, 2021 4:11:59 GMT -5
That's similar to what I have already done, I think. The code below produces this 8 communiy t 4 comuniy m 5 comuiy n 6 comui y 1 omui c 3 omi u 3 om i 2 o m 1 o It works nicely, because as you lose a character each time even if the same number comes up a few times you'll still end up with everything shuffled This works for me way I wanted above if I don't go past 9! Which is fine I don't need to, but on 10 it freezes a lot? odd! I have to CTRL/break to stop. ths73 - You say 'fill an array with numbers and shuffle it. I don't see how I shuffle it or do I just use random number generation to move them around within the array? I have achieved what I required with random generating in the word game I did, but was curious about other ways of doing it. I always seem to take the roundabout route! Brandon - I didn't really understand the LB version you suggested. I think I must be missing something there, but Thank you Thanks for your help Gentlemen
|
|
|
Post by tsh73 on Nov 30, 2021 4:24:39 GMT -5
Basically it's just boils to exactly the same algorythm you use. As Wikipedia puts it
So your version (first code) is perfectly OK.
I believe second vesion takes random numbers until it fills all places So to fill last remaining place it could do lot's of discards and if checking with INSTR you should take care to distinguish 1 from 10 I think it could hang just because of that. just run it under debugger 1) If first it gets 1 then it could put 10 after 2) But if first it get 10 then INSTR for 1 will return Found, so 1 will NEVER be added .. So you get endless loop.
Really, "drawing from a hat" is mach better approach.
|
|
dkl
Full Member
Posts: 234
|
Post by dkl on Nov 30, 2021 19:53:52 GMT -5
Ahha....thank you for the explanation. It's good to know why I was having problems with the second code, that really makes sense and I'm glad to know that my first code if fine
Is there an easier way of removing the chosen random element from the string rather than
1. storing the digits BEFORE the chosen random element (a$)
2. then storing the digits AFTER the chosen element to the end of the string (b$)
3. then concatenate the two strings c$ = a$ + b$
I don't think I can use replstr$ or remchar$ to remove the random element as it will immediately remove ALL versions of the element
|
|