|
Title: Pascal - No same numbers Post by: Nikica on May 14, 2009, 05:24:04 AM Can anyone please help me, I'm making a program in Pascal for Loto, 7 of 39, and I don't know how to make it that no same numbers appear. I made an algorithm to sort out the numbers for smaller to bigger;
Code: var I thought you can also do it like that for different number using 'p' to get out/stay in the loop but it seems that it's different, you would have to check all numbers from 1 to 7 and back again.b:array [1..7] of integer; k,p,km3:integer; begin writeln('LOTO COMBINATION: '); writeln; randomize; for k:=1 to 7 do b[k]:=random(39)+1; repeat p:=0; for k:= 1 to 6 do if b[k]>b[k+1] then begin km3:=b[k]; b[k]:=b[k+1]; b[k+1]:=km3; p:=1; end; until p=0; for k:=1 to 7 do writeln(b[k]); writeln; readln; end. Title: Re: Pascal - No same numbers Post by: Alex May on May 14, 2009, 05:34:20 AM How does this work in a real lottery situation? You have 39 balls, each with a different number on, and you pick seven out, one by one. When you remove a ball, it can't be picked again because it doesn't exist any more.
You problem is here: Code: for k:=1 to 7 do b[k]:=random(39)+1; How about this (I don't know if this is proper Pascal as I haven't used it in like 12 years) Code: for k:=1 to 7 do begin repeat b[k]:=random(39)+1; p:=0; for j:=1 to k do if b[k]=b[j] then p:=1; until p=0; end; This has a risk of infinite run time if the random function returns the same number infinitely, so you'd ideally code around that somehow. Title: Re: Pascal - No same numbers Post by: Glaiel-Gamer on May 14, 2009, 05:35:40 AM psuedo code:
do { pick a random number } while (number you picked is in the array of already picked numbers); put number in array return number Title: Re: Pascal - No same numbers Post by: Nikica on May 14, 2009, 05:54:48 AM How does this work in a real lottery situation? You have 39 balls, each with a different number on, and you pick seven out, one by one. When you remove a ball, it can't be picked again because it doesn't exist any more. This wouldn't work, I think mostly because you never defined b[j], the program compares b[k] to nothing.You problem is here: Code: for k:=1 to 7 do b[k]:=random(39)+1; How about this (I don't know if this is proper Pascal as I haven't used it in like 12 years) Code: for k:=1 to 7 do begin repeat b[k]:=random(39)+1; p:=0; for j:=1 to k do if b[k]=b[j] then p:=1; until p=0; end; This has a risk of infinite run time if the random function returns the same number infinitely, so you'd ideally code around that somehow. The problem is that same numbers can appear but you can't really make them non-exsit anymore you would have to ask yourslef if the numbers are same but it would be like that; If the first number generated is the same as the second then the second number is the first+1 but then you would have to ask yourself for the third number if it's same as the first and the second and so on, this would get really long and the numbers would likely be always +1, e.g. 10,11,12,13,14... Title: Re: Pascal - No same numbers Post by: Alex May on May 14, 2009, 06:16:21 AM Yes. One way around that is to reduce the number you are randomly picking each time.
so first time is random(39) + 1 second is random(38) + 1, and then if it is the same or larger than the first value, then add one to it number k is random(40 - k) + 1, and if it is the same as or larger than any of the values you already picked, add one for each time this is true. (b[j] is surely defined because j is always less than k? I'm sure you can understand my intent there anyway. It's just a guide.) Title: Re: Pascal - No same numbers Post by: PGGB on May 14, 2009, 07:06:21 AM Code: for k:=1 to 7 do begin repeat b[k]:=random(39)+1; p:=0; for j:=1 to k do if b[k]=b[j] then p:=1; until p=0; end; Code: for k:=1 to 7 do begin repeat b[k]:=random(39)+1; p:=0; for j:=1 to k-1 do if b[k]=b[j] then p:=1; until p=0; end; This will work. Title: Re: Pascal - No same numbers Post by: Alex May on May 14, 2009, 07:08:06 AM yeah, you get the point though. you bunch of pedants.
Title: Re: Pascal - No same numbers Post by: Impossible on May 14, 2009, 07:39:56 AM Hah, Pascal Loto. Sounds like a homework assignment, I remember that people would get banned from Gamedev.net for posting things like this :biglaff:
Title: Re: Pascal - No same numbers Post by: Michelle Disraeli on May 16, 2009, 03:03:27 AM Of course, there is a better way to do this that avoids infinite runtime...
For a relatively small set of potential objects, rather than just randomly picking one, you can shuffle the entire set. There's quite a few ways to do this (http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle), from ways that mimic how one might shuffle cards, to the rather quirky approach where you assign each element in the list a random number, then just sort according to the random number! :P |