Posted: Sun Oct 02, 2005 12:48 pm Post subject: Randint from a set of numbers?
Wow... it's been a long time since I've been here But anyways, to the problem:
I want my program to choose a random number from a list of numbers. Right now, I just want the list to be from 1-8, so the list (obviously) would be: 1,2,3,4,5,6,7,8. Now, I could just do this:
code:
randint (variable,1,8)
But, I want it so that it only picks each number once. So, I was wondering if there was a way, for example, when the program selects... let's say... 5, it would then drop 5 from the list of numbers to choose from. Thus, the list would now be: 1,2,3,4,6,7,8
If that wasn't specific enough, I'll try to go into further detail.
Sponsor Sponsor
beard0
Posted: Sun Oct 02, 2005 1:07 pm Post subject: (No subject)
Try putting your list of numbers in a flexible array (if you don't know what they are, the Turing help file is a good reference). Select a random number between lower(arrayName) and upper(arrayName), use that as your index. Then shuffle values down through your array, to replace the element you just "used", and remove the last element of your array - it is now ready for another selction.
skootles
Posted: Sun Oct 02, 2005 1:14 pm Post subject: (No subject)
Arrays... I'm just starting to use those with turing. I've used them in VB, but only lightly. Anyways, thanks, I'll look up flexible arrays.
[Gandalf]
Posted: Sun Oct 02, 2005 2:05 pm Post subject: (No subject)
Yep, I would do it like beard0 said. For any kind of 'list' you will for sure need to know arrays.
Cervantes
Posted: Sun Oct 02, 2005 6:46 pm Post subject: (No subject)
[Gandalf] wrote:
Yep, I would do it like beard0 said. For any kind of 'list' you will for sure need to know arrays.
Posted: Sun Oct 02, 2005 7:01 pm Post subject: (No subject)
Probably a better idea from a programming point of view, but the array solution is probably an easier one to use. For someone just starting to use arrays, pointers may be a little too complex.
[Gandalf]
Posted: Sun Oct 02, 2005 7:10 pm Post subject: (No subject)
Cervantes wrote:
[Gandalf] wrote:
Yep, I would do it like beard0 said. For any kind of 'list' you will for sure need to know arrays.
Posted: Sun Oct 02, 2005 7:11 pm Post subject: (No subject)
Ah, yes. I meant that solely as a comment to Gandalf. In fact, I was pondering whether to add a comment such as, "warning to skootles: clicking link may cause brain to explode".
Sponsor Sponsor
TokenHerbz
Posted: Sun Oct 02, 2005 9:08 pm Post subject: (No subject)
Array's are fairly simple, though the 2D arrays (which you dont need to know right now, but soon) can be a little more complicated...
Anyways, i wrote the code you strived for, so if your really stuck, just post here, and ill show you the code, and explain to you how it works...
But you must give it a good shot, its the only way to learn Good luck.!
Tony
Posted: Mon Oct 03, 2005 8:27 am Post subject: (No subject)
Posted: Mon Oct 03, 2005 1:23 pm Post subject: (No subject)
No ...
im not so good at making efficiant code... this was mine...
code:
var maxnum: int := 10 %%Enter here your max number
var number: int %%This here is your number
var used: array 1 .. maxnum of boolean %%this will tell you if the numbers used or not
for i : 1 .. maxnum %%use a for statment to declare the array variable's
used(i) := false %%sets all the numbers NOT declared
end for
loop %%to keep the program going
for i: 1 .. maxnum %%use this for cause it holds the variables need'd (booleans)
randint(number,1,maxnum) %%randomizes your number
if number = i then %% if the random number = the one in the for
if used(i) = false then %%checks to see if the numbers been used
put number %%puts the number
used(i) := true %%now marks the number as been used
end if
end if
end for
end loop
Mine might not be as efficiant, but it works, i have problems making efficiant code, and i think thats my weak point... I must learn, or think more about how i can accomplish this... Nice code though Tony.
codemage
Posted: Mon Oct 03, 2005 1:50 pm Post subject: (No subject)
This would be a slighly savvier method of implementing the main area of code, if you were to only use arrays. (The for & loop are switched, and there are a few other efficiency tweaks).
You essentially have parallel arrays here; one of them is a virtual array existing between 1 and maxnum.
Using arrays for this sort of code is very efficient with a small, consecutive array size. With really big nubmers, unsorted data types aren't so clean.
code:
for i : 1 .. maxnum
loop
number := Rand.Int (1, maxnum)
if used (number) = false then
put number
used (number) := true
end if
end loop
end for
beard0
Posted: Mon Oct 03, 2005 2:49 pm Post subject: (No subject)
codemage wrote:
This would be a slighly savvier method of implementing the main area of code, if you were to only use arrays.
Oh? Tony's is much better, as your method creates a random amount of wait time, incresing in expected duration each time through. It is actually quite inneficient.
Tony
Posted: Mon Oct 03, 2005 3:35 pm Post subject: (No subject)
codemage - the unsorted list of unique number is what we're after.
in your code forloop is not needed as the inside loop never exits
beard0 points out the core difference in approaches. The idea is that when randomly picking a number you have a deminishing chance of getting a number that wasn't used yet.
Example -- scramble 100 numbers in random order.
randomly picking 1st unsorted number - 100%
randomly picking 50th unsorted number - 50%
randomly picking 100th unsorted number - 1%
on average you make edit: 5 tries per number to be sorted (though you don't really notice that until you get into the last few numbers)
beard0
Posted: Mon Oct 03, 2005 7:23 pm Post subject: (No subject)
Tony wrote:
on average you make two tries per number to be sorted.
I get an average of 5.187378 tries per number to be sorted.
code:
var x : real := 0
for i : 1 .. 100
x += 100 / i
end for
put x/100