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

Username:   Password: 
 RegisterRegister   
 How to not repeat a number when using randint?
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2, 3  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TokenHerbz




PostPosted: 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! Smile
Sponsor
Sponsor
Sponsor
sponsor
GodKing




PostPosted: 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.
2goto1




PostPosted: 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.
TokenHerbz




PostPosted: Sun Jan 16, 2011 11:16 am   Post subject: RE:How to not repeat a number when using randint?

use arrays.
GodKing




PostPosted: 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?
TokenHerbz




PostPosted: 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.
GodKing




PostPosted: 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.
TokenHerbz




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




PostPosted: 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.
TokenHerbz




PostPosted: Sun Jan 16, 2011 12:11 pm   Post subject: RE:How to not repeat a number when using randint?

Turing:

%%so i tried to space everything out apart for you to understand how it all works... so here we go...

var dice_side : array 1 .. 6 of int  %%we need an array to store our random numbers we picked
var random_roll : int  %%and the random number ofcourse...

%%we wont use zero for our number im sure, so i just set the array of them to that, as no numbers are chosen yet
for i : 1 .. 6
    dice_side (i) := 0
end for

%%make a function to choose the (well mines a proc oh wells) lol -> to choose a random number.
proc Roll
    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
    %%store the random number to use later (should make this a fcn)
end Roll

%%now lets get to work!
for i : 1 .. 6 %%for each number we have to roll now
    Roll  %%we get our new number!
    dice_side (i) := random_roll  %%and assign it to our list!!!
end for

%%and now we can print our list, to show our random NON REPEATED NUMBERS!
for i : 1 .. 6
    put dice_side (i)
end for


%%now you should make that into a fcn, and compact it so it don't have so much clutter!
%%Be sure to post after i wanna see it!! Don't take me code copy/paste, write it yourself...
%%this is to show you kinda how it works to achieve what you want to do...

%%also keep in mind this isn't very efficiant, lets say we have 1000 numbers, can you imagine re-rolling over and over to his that LAST MISSING number?!
%it will take a while, so feel free to brain storm for the ideal way to solve this question...  :)
GodKing




PostPosted: 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?
GodKing




PostPosted: 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.
TokenHerbz




PostPosted: 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
TokenHerbz




PostPosted: 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
GodKing




PostPosted: 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?
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 2 of 3  [ 36 Posts ]
Goto page Previous  1, 2, 3  Next
Jump to:   


Style:  
Search: