Computer Science Canada How to not repeat a number when using randint? |
Author: | GodKing [ 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. |
Author: | Tony [ 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.
So why not do just that? |
Author: | 2goto1 [ 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? |
Author: | GodKing [ 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... |
Author: | Tony [ 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? |
Author: | GodKing [ 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 |
Author: | Tony [ 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? |
Author: | GodKing [ 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. |
Author: | Tony [ 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
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? |
Author: | GodKing [ 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
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 |
Author: | Tony [ 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:
Now after 4 we roll another number, this time it's 5. What do we do? |
Author: | GodKing [ Sun Jan 16, 2011 1:03 am ] | ||
Post subject: | Re: RE:How to not repeat a number when using randint? | ||
Tony @ Sun Jan 16, 2011 12:48 am wrote: 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:
Now after 4 we roll another number, this time it's 5. What do we do? Will it work if chose had an integer of 0? Elsif number =4 and number=5 and chosen =4 and chosen=5 then |
Author: | Tony [ Sun Jan 16, 2011 1:06 am ] |
Post subject: | RE:How to not repeat a number when using randint? |
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. |
Author: | GodKing [ 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. |
Author: | Tony [ 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. |
Author: | TokenHerbz [ Sun Jan 16, 2011 2:29 am ] |
Post subject: | RE:How to not repeat a number when using randint? |
i have this cat, and a dog, and a fish too... I use them like animal(1), animal(2), animal(3), What am i? im alot of different variables inside one! ![]() |
Author: | GodKing [ Sun Jan 16, 2011 9:50 am ] |
Post subject: | Re: RE:How to not repeat a number when using randint? |
Tony @ Sun Jan 16, 2011 1:46 am wrote: 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. I have no idea. I'm just not going to do it. I have no time. |
Author: | 2goto1 [ Sun Jan 16, 2011 11:00 am ] |
Post subject: | RE:How to not repeat a number when using randint? |
here's a hint. Each time you create a random number, store it somewhere. That way, the next time you create a new random number, you can look at the numbers that you've stored, to see if it's there. |
Author: | TokenHerbz [ Sun Jan 16, 2011 11:16 am ] |
Post subject: | RE:How to not repeat a number when using randint? |
use arrays. |
Author: | GodKing [ Sun Jan 16, 2011 11:19 am ] |
Post subject: | Re: RE:How to not repeat a number when using randint? |
2goto1 @ Sun Jan 16, 2011 11:00 am wrote: here's a hint. Each time you create a random number, store it somewhere. That way, the next time you create a new random number, you can look at the numbers that you've stored, to see if it's there.
How do I store a number and then exclude it out of the randint? |
Author: | TokenHerbz [ Sun Jan 16, 2011 11:36 am ] |
Post subject: | RE:How to not repeat a number when using randint? |
well lets random a number 1, 3 and we get 2. we store 2 so we know we have it. lets re-random a number 1 - 3, we get 2 again. lets check if we have 2, we DO! we better re roll, Wow nice we got freaking 1 this time, check, its different, lets store that one just like we did with 2, and repeat. however theres a better method, this is probly easiest for you. |
Author: | GodKing [ Sun Jan 16, 2011 11:40 am ] |
Post subject: | Re: RE:How to not repeat a number when using randint? |
TokenHerbz @ Sun Jan 16, 2011 11:36 am wrote: well lets random a number 1, 3 and we get 2.
we store 2 so we know we have it. lets re-random a number 1 - 3, we get 2 again. lets check if we have 2, we DO! we better re roll, Wow nice we got freaking 1 this time, check, its different, lets store that one just like we did with 2, and repeat. however theres a better method, this is probly easiest for you. Can you please show it like how it would look like in Turing. |
Author: | TokenHerbz [ Sun Jan 16, 2011 11:57 am ] |
Post subject: | RE:How to not repeat a number when using randint? |
well how would you do it in your head? or on paper? its pretty simple. roll a dice (6 sides) and to roll the rest of the numbers that arnt picked, you'd have to remove the chosen sides each time until no more are left right. give it a try and ill help you. |
Author: | GodKing [ Sun Jan 16, 2011 12:11 pm ] |
Post subject: | Re: RE:How to not repeat a number when using randint? |
[quote="TokenHerbz @ Sun Jan 16, 2011 11:57 am"]well how would you do it in your head? or on paper? its pretty simple. roll a dice (6 sides) and to roll the rest of the numbers that arnt picked, you'd have to remove the chosen sides each time until no more are left right. give it a try and ill help you.[/ Can you please show me? I tried and nothing is working. |
Author: | TokenHerbz [ Sun Jan 16, 2011 12:11 pm ] | ||
Post subject: | RE:How to not repeat a number when using randint? | ||
|
Author: | GodKing [ Sun Jan 16, 2011 12:47 pm ] |
Post subject: | Re: RE:How to not repeat a number when using randint? |
So in the part random_roll := Rand.Int (1, 6) %%we get this random number to use for check : 1 .. 6 %%now we have to check to vs the previous numbers we rolled if random_roll = dice_side (check) then %%if the numbers are the same, Roll %%we have to re-roll, and recheck (mise well recall our proc) end if end for It means that the check would go 1,2,3,4,5,6,. and if the random roll (which is randint) lands on the check then it will do the procedure again until it doesn't match? |
Author: | GodKing [ Sun Jan 16, 2011 1:03 pm ] |
Post subject: | Re: How to not repeat a number when using randint? |
I do not understand the dice+side(i):=random+roll part. |
Author: | TokenHerbz [ Sun Jan 16, 2011 1:15 pm ] |
Post subject: | RE:How to not repeat a number when using randint? |
Yes, But take note to the comments I wrote at the end of my example. You'd have to do this a different way if you where aiming for thousends of numbers |
Author: | TokenHerbz [ Sun Jan 16, 2011 1:23 pm ] |
Post subject: | RE:How to not repeat a number when using randint? |
well that means:: dice_side(i) is the array, and it equils the random number we get from the procedure we created. it would be easier to make that into a function and call it directly assigning the value that way, but maybe this way is easier for you to under stand first. functions sometimes are hard to understand for newcommers |
Author: | GodKing [ Sun Jan 16, 2011 3:42 pm ] |
Post subject: | Re: How to not repeat a number when using randint? |
I make it like this var equipment:int:=0 procedure randNum2 randint(equipment,1,10) end randNum2 Everytime I call randNum2, it might choose the same number. How do I not do this? |
Author: | Tony [ Sun Jan 16, 2011 3:59 pm ] |
Post subject: | RE:How to not repeat a number when using randint? |
That's what you've started with at the very beginning of this thread. I guess you could read the replies again? |
Author: | GodKing [ Sun Jan 16, 2011 4:04 pm ] |
Post subject: | Re: How to not repeat a number when using randint? |
Can anyone just show me so I can understand from it than pull out my hair? |
Author: | Tony [ Sun Jan 16, 2011 4:08 pm ] |
Post subject: | RE:How to not repeat a number when using randint? |
There's quite a lot of difference between "here's a complete solution to copy" and actually understanding the material well enough to write the solution yourself. Besides, TokenHerbz already provided a working example. |
Author: | Dragon20942 [ Tue Jan 18, 2011 7:44 pm ] |
Post subject: | RE:How to not repeat a number when using randint? |
My gr. 10 teacher didn't teach us about that particular type of data structure, thats why I had to ask you about that init. My knowledge of them was reverse engineered based on an example program. Either he/she doesn't know about them, then mercifully teach him/her about them and how to store each variable into it and run through it before randomizing (that was a hint). |
Author: | Tony [ Tue Jan 18, 2011 8:00 pm ] |
Post subject: | RE:How to not repeat a number when using randint? |
One doesn't _need_ arrays for this. They are just more convenient than having 10 separate variables. Alternatively, (because everybody knows of an int type), a single integer can be used as a bitfield (http://en.wikipedia.org/wiki/Bit_field); for just 10 numbers, the "bits" could remain entirely in decimal form too. Another neat solution in place of bitfields is a sum of primes. |
Author: | Coldkick [ Thu Jan 20, 2011 4:20 pm ] | ||
Post subject: | RE:How to not repeat a number when using randint? | ||
Ok, I'll lend a hand. First off, if you haven't worked with arrays, this may seem difficult but you will catch on. First we have to declare everything that will make the program work. What do we need? Well first and foremost you need it to be able to read a random number between 1 and 6 of a die. Next we need to somehow store the values already retrieved. For this we need to use an array. If you haven't used an array before here is the basic format. I won't give away what to put in each place though.
Now that we have declared the required variables we can move on to the actual program. The first thing that needs to occur is the rolling of the die. Next, we need to check and see if that number was found already. We do this by checking the array. What statement type can be used here to run through a minimum to a maximum number? If the number was found you can have it display a message, otherwise... We need it store the value of the die that was rolled. Display a message of what was rolled. That's my walk-through of everything that needs to be done. Hopefully its clear enough and can bring some understanding to the matter. |