Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411573 Posts in 69386 Topics- by 58444 Members - Latest Member: darkcitien

May 04, 2024, 02:52:20 PM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Pascal - No same numbers
Pages: [1]
Print
Author Topic: Pascal - No same numbers  (Read 1731 times)
Nikica
Level 5
*****


Remember, it’s all in your head.


View Profile
« 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
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.
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.
Logged
Alex May
...is probably drunk right now.
Level 10
*


hen hao wan


View Profile WWW
« Reply #1 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.
Logged

Glaiel-Gamer
Guest
« Reply #2 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
Logged
Nikica
Level 5
*****


Remember, it’s all in your head.


View Profile
« Reply #3 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.

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.
This wouldn't work, I think mostly because you never defined b[j], the program compares b[k] to nothing.
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...
« Last Edit: May 14, 2009, 06:03:07 AM by Nikica » Logged
Alex May
...is probably drunk right now.
Level 10
*


hen hao wan


View Profile WWW
« Reply #4 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.)
Logged

PGGB
Level 8
***



View Profile
« Reply #5 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;
This turns into an endless loop. b[k]=b[j] is always true when the for-loop ends as j equals k.

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.
Logged
Alex May
...is probably drunk right now.
Level 10
*


hen hao wan


View Profile WWW
« Reply #6 on: May 14, 2009, 07:08:06 AM »

yeah, you get the point though. you bunch of pedants.
Logged

Impossible
Level 3
***



View Profile
« Reply #7 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  Big Laff
Logged
Michelle Disraeli
Level 0
**


View Profile
« Reply #8 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, 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! Tongue
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic