Computer Science Canada

Super7

Author:  compudave [ Mon Nov 10, 2003 10:45 pm ]
Post subject:  Super7

Hi Smile I'm taking the grade 11 Computer Info & Sci course. I'd like some help with this program. This program is supposed to randomly choose 7 numbers, the only problem is sometimes these numbers are chosen twice.

My question is, what can I add that will make the program not choose the same number twice?

Quote:
randomize
var i : int
put "Super7 Lottery Number Chooser"
for count : 1 ..7
randint(i, 1, 47)
put i
end for

Author:  Chimaera [ Mon Nov 10, 2003 10:54 pm ]
Post subject: 

easy, just set up 7 randints separately and make it redo itself if it is equal to a number that has already been selected. Ta da! 8)

Author:  Tony [ Mon Nov 10, 2003 11:08 pm ]
Post subject: 

no no no Rolling Eyes tsk tsk tsk

you do not to that Rolling Eyes I've already wrote up the code for generating a list of random unique numbers, it is here

Author:  Chimaera [ Mon Nov 10, 2003 11:15 pm ]
Post subject: 

Here's exactly what your program should be doing

<code>
randomize
var i, a, b, c, d, e,f : int
put "Super7 Lottery Number Chooser"
randint (a, 1, 47)
randint (b, 1, 47)
randint (c, 1, 47)
randint (d, 1, 47)
randint (e, 1, 47)
randint (f, 1, 47)
randint (i, 1, 47)
loop
if a = b or a = c or a = d or a = e or a = f or a = i
then
randint (a, 1, 47)
elsif b = a or b = c or b = d or b = e or b = f or b = i
then
randint (b, 1, 47)
elsif c = b or c = a or c = d or c = e or c = f or c = i
then
randint (c, 1, 47)
elsif d = b or d = c or d = a or d = e or d = f or d = i
then
randint (d, 1, 47)
elsif e = b or e = c or e = a or e = d or e = f or e = i
then
randint (e, 1, 47)
elsif f = b or f = c or f = a or f = e or f = d or f = i
then
randint (f, 1, 47)
elsif i = b or i = c or i = a or i = e or i = f or i = d
then
randint (i, 1, 47)
end if
exit when a not= b and a not= c and a not= d and a not= e and a not= f and a not= i
and b not= a and b not= c or b not= d and b not= e and b not= f and b not= i
and c not= b and c not= a and c not= d and c not= e and c not= f and c not= i
and d not= b and d not= c and d not= a and d not= e and d not= f and d not= i
and e not= b and e not= c and e not= a and e not= d and e not= f and e not= i
and f not= b and f not= c and f not= a and f not= e and f not= d and f not= i
and f not= b and f not= c and f not= a and f not= e and f not= d and f not= i
end loop
put a, " ", b, " ", c, " ", d, " ", e, " ", f, " ", i

</code>

Author:  PaddyLong [ Tue Nov 11, 2003 1:06 pm ]
Post subject: 

ohhh please noooo don't do it the way Chimaera says... that way is horrible... using wayyy more variables than you need.... sure that way works but it's not a very good way to do it

Author:  compudave [ Tue Nov 11, 2003 1:28 pm ]
Post subject: 

lol, yeah thanks for the coding but that's a lot of variables... i'm going to try and work with the other code...

Author:  AsianSensation [ Tue Nov 11, 2003 7:21 pm ]
Post subject: 

lol, Chimaera, what if someone wanted 100 different numbers?

by your method, then it would take (100(101))/2, or 5050 different comparisons.

codes have to be effiecient and clean,, and usually have to deal with any situation of it's type.

Author:  Tony [ Tue Nov 11, 2003 7:29 pm ]
Post subject: 

doesn't anyone ever look at my code Confused I'll post it here, not just the link this time
code:

var randN:int %just to hold the number
var numbers:array 1..47 of int
for i:1..47
numbers(i):=i
end for

for decreasing i:47..40
randN := Rand.Int(1,i)
put numbers(randN) , " "..
    if randN not=i then
    for a:randN+1..i
    numbers(a-1):=numbers(a)
    end for
    end if
end for

Author:  Chimaera [ Tue Nov 11, 2003 9:04 pm ]
Post subject: 

wow I am so shamed by tony =_= well actually tony didn't shame me, I shamed myself by putting up my cruddy code. Crying or Very sad I don't really think in terms of that, I just think of doing what the question asks specifically as opposed to allowing it to be more open ended and perhaps using it for other things in the future. Props to tony for doing that stuff though

Author:  Tony [ Tue Nov 11, 2003 11:08 pm ]
Post subject: 

heh, dont worry about that Chimaera Wink

this comes with experience. Everybody starts off with programing a program that acomplishes a task, but with experience and knowledge, you'll begin to see a bigger picture. And as far as I know, when it comes to programming - there's a LOT of changes between original goal and final product.

Heh - here's an interesting consept you'll come across if you keep on programming long enough : [b]abstract classes[/i]. I learned them in Java 8) They are basically object parts that cannot be used directly, but other objects incorporate in themselves. Though you'd probly need to get into OOP first to understand.

Author:  Chimaera [ Tue Nov 11, 2003 11:27 pm ]
Post subject: 

btw what is this OOP stuff that you guys talk about sometimes. I've heard you guys use the term, but still have no idea just what it is.

Author:  Tony [ Tue Nov 11, 2003 11:32 pm ]
Post subject: 

OOP - Object Oriented Programming. It's a style of programming where you create interactive objects, rether then one continues code that flows one way.

It's possible to use OOP in Turing (WinOOT - Windows Object Oriented Turing Rolling Eyes but it's all a big lie. Noone but Catalyst uses OOP in turing Laughing), you might touch onto it in C++ (I didn't Sad) but Java is where the fun is at Very Happy in Java, everything is OOP. Heck, even your variables are objects - each containing it's own properties and methods that can be executed.

Author:  Blade [ Tue Nov 11, 2003 11:45 pm ]
Post subject: 

for the answer to the question... wouldn't it be much better to use a boolean array? because they way tony's doing it you are running a for loop a hella lotta times... maximizing execution time..

Author:  Tony [ Tue Nov 11, 2003 11:53 pm ]
Post subject: 

blade, you must be on drugs or something Confused
Quote:
for decreasing i:47..40


thats 7 executions, exactly how many random numbers are needed Rolling Eyes

first for loop is just initialization of the array, something you would also need for a boolean array Confused As I said this before - to randomly pick 1000th unique number, you would have a 1/1000 chance of landing the right number and a hell lots of if statments executed using boolean array method:lol:

Author:  Blade [ Wed Nov 12, 2003 12:50 pm ]
Post subject: 

code:
const num:10
var ii,temp:int:=0
var nums:array 1.. num of int
var bool:array 1.. num of boolean

for i:1..num
  bool(i):=false
end for

loop
  temp:=Rand.Int(1,num)
  if (bool(temp) = false) then
    bool(temp):=true
    nums(ii):=temp
    ii+=1
  end if
  exit when ii >= num
end loop


if your numbers are between like 30 and 40 then chance your boolean array to 30 .. 40 and your Rand.Int between 30 and 40

it works for everything, and its simpler than yours, but i dont know about execution time

Author:  Catalyst [ Wed Nov 12, 2003 1:05 pm ]
Post subject: 

tony- u should look at the OOP in c++, it might not be as "correct" as it is in java but it is really powerful (mutiple inheritance, abstract functions/classes, etc)

Author:  smith_scott_04 [ Wed Nov 12, 2003 6:38 pm ]
Post subject:  I Have Already Wrote The Program

Here is the lotto 649 game just change a few variables.

code:


var num : array 1 .. 6 of int
var number : array 1 .. 6 of int
var tot : int := 0
var finish : boolean

%-----------------------------------------------------------

procedure RandNum ()
    var doesrepeat : boolean
    randint (num (1), 1, 49)
    for cRandNum : 2 .. 6
        loop
            doesrepeat := false
            randint (num (cRandNum), 1, 49)
            for cNewRand : 1 .. cRandNum - 1
                if num (cRandNum) = num (cNewRand) then
                    doesrepeat := true
                end if
            end for
            exit when doesrepeat = false
        end loop
    end for
end RandNum

%-----------------------------------------------------------

procedure ShowNum

    locate (9, 2)
    put "The Numbers Were: "
    locate (10, 2)
    color (red)
    put num (1), " - " ..
    put num (2), " - " ..
    put num (3), " - " ..
    put num (4), " - " ..
    put num (5), " - " ..
    put num (6)

    finish := true

end ShowNum

%-----------------------------------------------------------

procedure Guess

    for locate1 : 3 .. 52 by 5

        tot := tot + 1
        loop
            var doesrepeat : boolean := false
            loop
                locate (3, 1)
                color (255)
                put "Choose 6 Numbers (1,49): " ..
                drawfillbox (200, 350, 400, 370, white)
                get number (tot)
                doesrepeat := false
                if tot > 1 then
                    for c1 : 1 .. tot - 1
                        if number (c1) = number (tot) then
                            doesrepeat := true
                        end if
                    end for
                else
                    exit
                end if
                exit when doesrepeat = false
            end loop
            if number (tot) <= 49 then
                if tot < 6 then
                    locate (5, locate1)
                    put number (tot), " - "
                end if

                if tot > 5 then
                    locate (5, locate1)
                    put number (tot)
                end if
                exit
            end if
        end loop

        if tot = 6 then
            exit
        end if

    end for

end Guess

%-----------------------------------------------------------

procedure CheckNums

    var wrong : int := 0

    for count : 1 .. 6
        for c1 : 1 .. 6
            if num (count) = number (c1) then
                wrong := wrong + 1
            end if
        end for
    end for

    locate (7, 2)
    put "You got: ", (wrong), " right"
end CheckNums

%-----------------------------------------------------------

loop

    locate (1, 28)
    color (blue)
    put "Lotto 649 - Just Imagine"

    color (255)

    RandNum
    Guess
    CheckNums
    ShowNum

    delay (10000)
    cls
    tot := 0
end loop


Author:  Chimaera [ Wed Nov 12, 2003 7:34 pm ]
Post subject: 

you can't exit your program without doing it manually. Perhaps you should modify your program to do so, but other than that nice program. It doesn't have much to do with Super 7 though.

Author:  Tony [ Wed Nov 12, 2003 11:52 pm ]
Post subject: 

blade - obviously mine is simpler AND has better execution time... has less lines of code too Laughing

Author:  compudave [ Thu Nov 13, 2003 12:52 am ]
Post subject: 

Laughing hehe, You said it Tony...

Author:  hackman [ Thu Nov 13, 2003 11:40 am ]
Post subject: 

I had to do the same program, my teacher was happy with a simple

code:
put "Would you like to re-generate the numbers? <y/n>"
if ans="n" then
...
...
...
...

Author:  Tony [ Thu Nov 13, 2003 3:18 pm ]
Post subject: 

well the chances of
code:

for i:1..10
put Rand.Int(1,10)
end for


to generate a list of 10 unique numbers...

is 1 in 10*9*8...*1
Twisted Evil


: