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

Username:   Password: 
 RegisterRegister   
 Super7
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
compudave




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Chimaera




PostPosted: Mon Nov 10, 2003 10:54 pm   Post subject: (No 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)
Tony




PostPosted: Mon Nov 10, 2003 11:08 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Chimaera




PostPosted: Mon Nov 10, 2003 11:15 pm   Post subject: (No 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>
PaddyLong




PostPosted: Tue Nov 11, 2003 1:06 pm   Post subject: (No 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
compudave




PostPosted: Tue Nov 11, 2003 1:28 pm   Post subject: (No 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...
AsianSensation




PostPosted: Tue Nov 11, 2003 7:21 pm   Post subject: (No 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.
Tony




PostPosted: Tue Nov 11, 2003 7:29 pm   Post subject: (No 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
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Chimaera




PostPosted: Tue Nov 11, 2003 9:04 pm   Post subject: (No 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
Tony




PostPosted: Tue Nov 11, 2003 11:08 pm   Post subject: (No 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Chimaera




PostPosted: Tue Nov 11, 2003 11:27 pm   Post subject: (No 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.
Tony




PostPosted: Tue Nov 11, 2003 11:32 pm   Post subject: (No 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.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Blade




PostPosted: Tue Nov 11, 2003 11:45 pm   Post subject: (No 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..
Tony




PostPosted: Tue Nov 11, 2003 11:53 pm   Post subject: (No 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:
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Blade




PostPosted: Wed Nov 12, 2003 12:50 pm   Post subject: (No 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
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 2  [ 22 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: