
-----------------------------------
KONjbnj
Fri Apr 28, 2006 1:26 pm

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.

-----------------------------------
Albrecd
Fri Apr 28, 2006 1:32 pm


-----------------------------------
Use a for loop for your if statement between the two rolls.

%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?

-----------------------------------
KONjbnj
Fri Apr 28, 2006 1:34 pm


-----------------------------------
Whoops, I mean that the second variable can't be the same as the first.

-----------------------------------
KONjbnj
Fri Apr 28, 2006 1:35 pm


-----------------------------------
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.

-----------------------------------
Albrecd
Fri Apr 28, 2006 1:38 pm


-----------------------------------
Wow, I feel stupid  :x .

Just get rid of the line:


for loop determining what second randint can't be 


-----------------------------------
KONjbnj
Fri Apr 28, 2006 1:40 pm


-----------------------------------
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)


    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

-----------------------------------
Albrecd
Fri Apr 28, 2006 1:59 pm


-----------------------------------
exit when PC2 not= PC1 and PC2S not= PC1S 
end loop

It should be or, rather than and.

-----------------------------------
Cervantes
Fri Apr 28, 2006 2:56 pm


-----------------------------------
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.


var n := 13

var d1 := Rand.Int (1, n)
var d2 := Rand.Int (1, n - 1)
if d2 = d1 then
    d2 := n
end if

