Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 When randomizing numbers, how do I prevent the number to reoccur?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Wintermini




PostPosted: Tue Jan 12, 2010 6:33 pm   Post subject: When randomizing numbers, how do I prevent the number to reoccur?

What is it you are trying to achieve?
I'm trying to create a quiz Smile I got the randomizing questions part with the help of arrays, but sometimes, the question will repeat itself. I don't want that.


What is the problem you are having?
- questions (or numbers either way...) are repeated -__-
- how do i calculate the score for this quiz as well?
- how do I stop this quiz?!?! T_T omg i'm so lost and helpless Crying or Very sad


Describe what you have tried to solve this problem
I tried to take out the looping process out of the code, but I don't think it's helping :\


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Turing:


var q : array 0 .. 5 of int

q(0) := Pic.FileNew("q3.bmp")
q(1) := Pic.FileNew("q4.bmp")
q(2) := Pic.FileNew("q6.bmp")
q(3) := Pic.FileNew("q7.bmp")
q(4) := Pic.FileNew("q8.bmp")
q(5) := Pic.FileNew("q9.bmp")
if x >= 450 and x <= 800 and y >= 400 and y <= 450 then 
    pic:= Pic.FileNew ("quizbackground2.jpg")
    if pic = 0 then
    put "Unable to load JPG: ", Error.LastMsg
    return
    end if   
    Pic.Draw (pic, 0, 0, picCopy)
   
    var tmp : int := Rand.Int(0, 5)
    Pic.Draw(q(tmp), 300, 200, picMerge)

    loop
    if tmp = 0 then
        Draw.Box (300, 200, 510, 250, black)
            loop
              Pic.Draw(q(0), 300, 200, picMerge)
              buttonwait ("down", x, y, bnum, bud)
              cls
              if not (x >= 300 and x <= 510 and y >= 200 and y <= 250) then
                  pic:= Pic.FileNew ("quizbackground2.jpg")
                  if pic = 0 then
                  put "Unable to load JPG: ", Error.LastMsg
                  return
                  end if   
                  Pic.Draw (pic, 0, 0, picCopy)         
                  pic:= Pic.FileNew ("hintq3.bmp")
                  if pic = 0 then
                  put "Unable to load JPG: ", Error.LastMsg
                  return
                  end if   
                  Pic.Draw (pic, 200, 400, picMerge)
                  Music.PlayFile("boo.wav")           
                  delay (2000)
                  cls
                  pic:= Pic.FileNew ("quizbackground2.jpg")
                  if pic = 0 then
                  put "Unable to load JPG: ", Error.LastMsg
                  return
                  end if   
                  Pic.Draw (pic, 0, 0, picCopy)                 
                  tmp := Rand.Int(0, 5)
                  Pic.Draw(q(tmp), 300, 200, picMerge)
              end if
           
            exit when x >= 300 and x <= 510 and y >= 200 and y <= 250
            end loop
            if x >= 300 and x <= 510 and y >= 200 and y <= 250 then
                cls
                pic:= Pic.FileNew ("quizbackground2.jpg")
                if pic = 0 then
                put "Unable to load JPG: ", Error.LastMsg
                return
                end if   
                Pic.Draw (pic, 0, 0, picCopy) 
                tmp := Rand.Int(1, 5)
                Pic.Draw(q(tmp), 300, 200, picMerge)
            end if
    end if
   
    if tmp = 1 then
        Draw.Box (310, 270, 625, 330, black)
        loop
           Pic.Draw(q(1), 300, 200, picMerge)
           buttonwait ("down", x, y, bnum, bud)
           cls
           if not (x >= 310 and x <= 625 and y >= 270 and y <= 330) then
               pic:= Pic.FileNew ("quizbackground2.jpg")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 0, 0, picCopy)         
               pic:= Pic.FileNew ("hintq4.bmp")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 200, 400, picMerge)
               Music.PlayFile("boo.wav")           
               delay (2000)
               cls
           end if
           
         exit when x >= 310 and x <= 625 and y >= 270 and y <= 330
         end loop
            if x >= 310 and x <= 625 and y >= 270 and y <= 330 then
                cls
                pic:= Pic.FileNew ("quizbackground2.jpg")
                if pic = 0 then
                put "Unable to load JPG: ", Error.LastMsg
                return
                end if   
                Pic.Draw (pic, 0, 0, picCopy)
                tmp := Rand.Int(2, 5)
                Pic.Draw(q(tmp), 300, 200, picMerge)
            end if         
    end if
   
    if tmp = 2 then
        Draw.Box (300, 350, 770, 400, black)
        loop
           Pic.Draw(q(2), 300, 200, picMerge)
           buttonwait ("down", x, y, bnum, bud)
           cls
           if not (x >= 300 and x <= 770 and y >= 350 and y <= 400) then
               pic:= Pic.FileNew ("quizbackground2.jpg")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 0, 0, picCopy)         
               pic:= Pic.FileNew ("hintq6.bmp")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 200, 400, picMerge)
               Music.PlayFile("boo.wav")           
               delay (2000)
               cls
               pic:= Pic.FileNew ("quizbackground2.jpg")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 0, 0, picCopy)               
           end if
           
       exit when x >= 300 and x <= 770 and y >= 350 and y <= 400
       end loop
            if x >= 300 and x <= 770 and y >= 350 and y <= 400 then
                cls
                pic:= Pic.FileNew ("quizbackground2.jpg")
                if pic = 0 then
                put "Unable to load JPG: ", Error.LastMsg
                return
                end if   
                Pic.Draw (pic, 0, 0, picCopy) 
                tmp := Rand.Int(3, 5)
                Pic.Draw(q(tmp), 300, 200, picMerge)
            end if           
    end if
   
    if tmp = 3 then
        Draw.Box (280, 420, 700, 470, black)
        loop
           Pic.Draw(q(3), 300, 200, picMerge)
           buttonwait ("down", x, y, bnum, bud)
           cls
           if not (x >= 280 and x <= 700 and y >= 420 and y <= 470) then
               pic:= Pic.FileNew ("quizbackground2.jpg")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 0, 0, picCopy)         
               pic:= Pic.FileNew ("hintq7.bmp")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 200, 400, picMerge)
               Music.PlayFile("boo.wav")           
               delay (2000)
               cls
               pic:= Pic.FileNew ("quizbackground2.jpg")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 0, 0, picCopy)               
           end if
           
         exit when x >= 280 and x <= 700 and y >= 420 and y <= 470
         end loop
            if x >= 280 and x <= 700 and y >= 420 and y <= 470 then
                cls
                pic:= Pic.FileNew ("quizbackground2.jpg")
                if pic = 0 then
                put "Unable to load JPG: ", Error.LastMsg
                return
                end if   
                Pic.Draw (pic, 0, 0, picCopy) 
                tmp := Rand.Int(4, 5)
                Pic.Draw(q(tmp), 300, 200, picMerge)
            end if         
    end if
    if tmp = 4 then
        Draw.Box (300, 350, 720, 400, black)
        loop
           Pic.Draw(q(4), 300, 200, picMerge)
           buttonwait ("down", x, y, bnum, bud)
           cls
           if not (x >= 300 and x <= 720 and y >= 350 and y <= 400) then
               pic:= Pic.FileNew ("quizbackground2.jpg")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 0, 0, picCopy)         
               pic:= Pic.FileNew ("hintq8.bmp")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 200, 400, picMerge)
               Music.PlayFile("boo.wav")           
               delay (2000)
               cls
               pic:= Pic.FileNew ("quizbackground2.jpg")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 0, 0, picCopy)               
           end if
           
         exit when x >= 300 and x <= 720 and y >= 350 and y <= 400
         end loop
            if x >= 300 and x <= 720 and y >= 350 and y <= 400 then
                cls
                pic:= Pic.FileNew ("quizbackground2.jpg")
                if pic = 0 then
                put "Unable to load JPG: ", Error.LastMsg
                return
                end if   
                Pic.Draw (pic, 0, 0, picCopy)
                tmp := Rand.Int(5, 5)
                Pic.Draw(q(tmp), 300, 200, picMerge)
            end if               
    end if
    if tmp = 5 then
     Draw.Box (285, 200, 540, 250, black)
        loop
           Pic.Draw(q(5), 300, 200, picMerge)
           buttonwait ("down", x, y, bnum, bud)
           cls
           if not (x >= 285 and x <= 540 and y >= 200 and y <= 250) then
               pic:= Pic.FileNew ("quizbackground2.jpg")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 0, 0, picCopy)         
               pic:= Pic.FileNew ("hintq9.bmp")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 200, 400, picMerge)
               Music.PlayFile("boo.wav")           
               delay (2000)
               cls
               pic:= Pic.FileNew ("quizbackground2.jpg")
               if pic = 0 then
               put "Unable to load JPG: ", Error.LastMsg
               return
               end if   
               Pic.Draw (pic, 0, 0, picCopy)               
           end if
           
         exit when x >= 285 and x <= 540 and y >= 200 and y <= 250
         end loop
            if x >= 285 and x <= 540 and y >= 200 and y <= 250 then
                cls
                pic:= Pic.FileNew ("quizbackground2.jpg")
                if pic = 0 then
                put "Unable to load JPG: ", Error.LastMsg
                return
                end if   
                Pic.Draw (pic, 0, 0, picCopy)     
                tmp := Rand.Int(0, 5)
                Pic.Draw(q(tmp), 300, 200, picMerge)
            end if
        end if
        end loop





Please specify what version of Turing you are using
I'm using Turing 4.0.3 (the only version my school provided) Sad
Sponsor
Sponsor
Sponsor
sponsor
Kharybdis




PostPosted: Tue Jan 12, 2010 7:49 pm   Post subject: RE:When randomizing numbers, how do I prevent the number to reoccur?

Well, to prevent a number from reoccurring, you can just randomize the order itself in the beginning, and use a loop with that new order. To calculate the score on a quiz, if the person gets it right, you assign + 1 to a counting variable. If a person gets it wrong, you assign the counting variable -1.

Turing:
var count := 0
if right then
count +=1
elsif wrong then
count -=1
end if


I don't quite see a way for the user to respond to your program, but i did glance over it just briefly. One way to exit your program would be to have the whole program in a loop that ends when 5 questions have been asked or when the user types a letter or any other input that symbolizes an exit command, at which point the program exits.
TerranceN




PostPosted: Tue Jan 12, 2010 8:16 pm   Post subject: RE:When randomizing numbers, how do I prevent the number to reoccur?

I would reccomend you make an array of boolean values, the same size as your questions array, so that when you ask a question you can mark that you have already done that question. Then you can put your random number assignment in a loop and only exit when the number has not already been asked (just make sure to also have a way to check when all of the questions have been asked). For the score, that is just a global number that you increment when someone gets a right answer.
Insectoid




PostPosted: Tue Jan 12, 2010 8:19 pm   Post subject: RE:When randomizing numbers, how do I prevent the number to reoccur?

That's way too complicated TerranceN. A better way is to have an array and select 2 random elements and swap them, then do it again, and again, and again, until it's nicely shuffled, then ask in that order.
TerranceN




PostPosted: Tue Jan 12, 2010 8:36 pm   Post subject: Re: When randomizing numbers, how do I prevent the number to reoccur?

I know it's not the most efficient way, but it was what I used when I was learning turing, and since Wintermini's code is pretty much brute forcing through the problem, I figured a conceptually simple answer (at least it seems that way to me) that is inefficient would be better/more helpful then an answer that requires more thinking and is more efficient.

That raises a good question, though, is it better to teach the 'most efficient answer', or teach the basic answer, and let students figure out better answers on their own? I have learned through the second way, so thats probably why I default to it.

I'm running on low sleep, so I'm sorry if that makes no sense.
DemonWasp




PostPosted: Tue Jan 12, 2010 10:39 pm   Post subject: RE:When randomizing numbers, how do I prevent the number to reoccur?

In general, one hopes to educate the student by providing first a simple, direct approach, then a faster approach. In some cases, the two are the same.

In this particular case, both have reasonable analogies. TerranceN's method is equivalent to writing down the numbers 1 to N, then crossing off the ones that you randomly generate. If a number is already crossed off, you reroll. insectoid's method is akin to shuffling a deck of cards and drawing from the top, discarding cards as you go - you'll never get the same card (or number) twice.
Ktomislav




PostPosted: Wed Jan 13, 2010 8:33 am   Post subject: Re: When randomizing numbers, how do I prevent the number to reoccur?

I think this is the best way to go:
Random a number from one to the number of questions.
Write the question, user input and that stuff.
Swap the last question and the question you have just randomed.
Decrease the number of questions.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: