Posted: Sat Jan 15, 2011 10:11 pm Post subject: How to not repeat a number when using randint?
I need a number from 1-10 to randomize itself but once it has been randomized already, it won't include that number.
For example, if it chose 4, then it will randomize everything from 1-10 except 4. Then the next number like 5 and it won't randomize any number from 1-10 except for 4 and 5.
Sponsor Sponsor
Tony
Posted: Sat Jan 15, 2011 10:15 pm Post subject: Re: How to not repeat a number when using randint?
GodKing @ Sat Jan 15, 2011 10:11 pm wrote:
For example, if it chose 4, then it will randomize everything from 1-10 except 4.
Posted: Sat Jan 15, 2011 11:13 pm Post subject: RE:How to not repeat a number when using randint?
Why not try keeping tracking of the random numbers that you have created? Wouldn't that be useful to check?
GodKing
Posted: Sat Jan 15, 2011 11:17 pm Post subject: Re: How to not repeat a number when using randint?
I don't know how to do that...
Tony
Posted: Sat Jan 15, 2011 11:54 pm Post subject: Re: How to not repeat a number when using randint?
GodKing @ Sat Jan 15, 2011 11:17 pm wrote:
I don't know how to do that...
That's what you're here to figure out, right?
Lets start with the basics. We have picked only one random number so far, and it is obviously 4 (http://xkcd.com/221/). What are all of the different ways that we can keep track of the information that the number 4 has been picked?
Posted: Sat Jan 15, 2011 11:57 pm Post subject: Re: How to not repeat a number when using randint?
Tony @ Sat Jan 15, 2011 11:54 pm wrote:
GodKing @ Sat Jan 15, 2011 11:17 pm wrote:
I don't know how to do that...
That's what you're here to figure out, right?
Lets start with the basics. We have picked only one random number so far, and it is obviously 4 (http://xkcd.com/221/). What are all of the different ways that we can keep track of the information that the number 4 has been picked?
How about something like
if number=4 then
I have no idea what to put here!
End if
Tony
Posted: Sun Jan 16, 2011 12:13 am Post subject: RE:How to not repeat a number when using randint?
Think harder. How do you know that 4 was rolled in the first place?
Posted: Sun Jan 16, 2011 12:20 am Post subject: Re: RE:How to not repeat a number when using randint?
[quote="Tony @ Sun Jan 16, 2011 12:13 am"]Think harder. How do you know that 4 was rolled in the first place?[/quote
We can write
Randint(number1,10)
Put number
If number=4 then
Put "this is 4"
I don't know what to put here, still.
End if.
Sponsor Sponsor
Tony
Posted: Sun Jan 16, 2011 12:29 am Post subject: RE:How to not repeat a number when using randint?
reference based procedures are ugly, lets use proper functions. This can be written as
Turing:
var number:int
number := Rand.Int(1,10) if number =4then % variable number has value 4 endif
So at the moment we have a variable "number" and it knows that we rolled a 4. Presumably we will use the same "number" to keep on picking out other numbers. Can we use some variable other than "number" to remember that 4 has been picked?
Posted: Sun Jan 16, 2011 12:38 am Post subject: Re: RE:How to not repeat a number when using randint?
Tony @ Sun Jan 16, 2011 12:29 am wrote:
reference based procedures are ugly, lets use proper functions. This can be written as
Turing:
var number:int
number := Rand.Int(1,10) if number =4then % variable number has value 4 endif
So at the moment we have a variable "number" and it knows that we rolled a 4. Presumably we will use the same "number" to keep on picking out other numbers. Can we use some variable other than "number" to remember that 4 has been picked?
How about the variable var chosen:int:=4
Tony
Posted: Sun Jan 16, 2011 12:48 am Post subject: RE:How to not repeat a number when using randint?
Ok, that's one way to do it. It could work too, but we might need to be just a bit more tricky with it.
First, now that we have value 4 in "chosen", and we roll a 4 in "number" again, how do we check that this already happened? Should be trivially:
code:
if number = 4 and chosen = 4 then
% 4 already happened
Now after 4 we roll another number, this time it's 5. What do we do?
Posted: Sun Jan 16, 2011 1:41 am Post subject: Re: RE:How to not repeat a number when using randint?
Tony @ Sun Jan 16, 2011 1:06 am wrote:
if "chosen" was not 4, then the entire statement would not be true, so it will not get in.
Quote:
Elsif number =4 and number=5 ...
number can never be both 4 _and_ 5 at the same time.
I'm really desperate here. This is like 20% of my marks and this is the last thing I have to do.
Can you please be a kind person and lend me a hand?
I need te program to remember what it randomized everytime.
If it chose 4 and 5 I don't want it to choose 4 or 5.
I don't know how it looks like.
I have tried asking all my friends and look up Turing help. There is No way to do this.
Tony
Posted: Sun Jan 16, 2011 1:46 am Post subject: RE:How to not repeat a number when using randint?
We started going in the right direction.
You just need to figure out what data structure (arrangement of one or more variables of some types) can hold the information that 4 has been chosen, then (4,5), and ultimately the entire set.
Think about all the different variable types that you know, and which ones of them could be used for something like this.