Computer Science Canada

Rand.Int help

Author:  KONjbnj [ Fri Apr 28, 2006 1:26 pm ]
Post subject:  Rand.Int help

I have two variables that I am giving a random number from 1-13. I want it so that if the first variable is say, 6, the second variable can't be five.

I though of using an 'if' statement, but I just get even more confused.

Author:  Albrecd [ Fri Apr 28, 2006 1:32 pm ]
Post subject: 

Use a for loop for your if statement between the two rolls.

code:
%get first randint

for loop determining what second randint can't be

%get second randint

%if the second randint is what it shouldn't be, roll it again (you'll probably want this in a conditional loop)


By the way, what are you using this for?

Author:  KONjbnj [ Fri Apr 28, 2006 1:34 pm ]
Post subject: 

Whoops, I mean that the second variable can't be the same as the first.

Author:  KONjbnj [ Fri Apr 28, 2006 1:35 pm ]
Post subject: 

And it's for some blackjack game I gotta do for class.

I want it so that if the first card is Ace of Spades, no other card can be Ace of Spades.

Author:  Albrecd [ Fri Apr 28, 2006 1:38 pm ]
Post subject: 

Wow, I feel stupid Mad .

Just get rid of the line:

Albrecd in his stupidity wrote:

for loop determining what second randint can't be

Author:  KONjbnj [ Fri Apr 28, 2006 1:40 pm ]
Post subject: 

Thanks, I think I got it.

PC1 and 2 is first and second card.
PC1S and 2S is the first and second suit (1 = Spades, 2 = Hearts, 3 Clubs, 4 = Diamond)

code:

    PC1 := Rand.Int (1, 13)
    PC1S := Rand.Int (1, 4)
    PC2 := Rand.Int (1, 13)
    PC2S := Rand.Int (1, 4)
    loop
        if PC2 = PC1 and PC2S = PC1S then
            PC2 := Rand.Int (1, 13)
            PC2S := Rand.Int (1, 4)
        end if
        exit when PC2 not= PC1 and PC2S not= PC1S
    end loop

Author:  Albrecd [ Fri Apr 28, 2006 1:59 pm ]
Post subject: 

Quote:
code:
exit when PC2 not= PC1 and PC2S not= PC1S
end loop


It should be or, rather than and.

Author:  Cervantes [ Fri Apr 28, 2006 2:56 pm ]
Post subject: 

To return to the original question for a moment, there's no need to have a loop. Why would you possibly want to use a loop to generate a second random number when you can do it on the first try?

Say the first roll is 6. The second roll must be one of the set {1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 13}. There's 12 digits there. Thus, we can "roll a 12-sided die" and use that value. However, if the value on this roll is a 6, we return 13.

code:

var n := 13

var d1 := Rand.Int (1, n)
var d2 := Rand.Int (1, n - 1)
if d2 = d1 then
    d2 := n
end if


: