Computer Science Canada

Random integer help

Author:  KONjbnj [ Tue May 03, 2005 7:44 am ]
Post subject:  Random integer help

How do you get a random integer number, but it's only the even numbers? I so far have:

var num : int
randomize
randint (num, 1, 50)
put num

But I don't know what to do next.



Also, for one of the questions, I'm supposed to scramble a word and let the user guess what the word is. I'm really confused at how to approach this.

[i]Edit:[/i]Nevermind, I figured out the even number thing. I used:

[quote] var num : int
var even : int := 1
randomize
loop
randint (num, 1, 50)
even := num mod 2
if even = 0 then
put num
end if
exit when even = 0
end loop[/quote]

Author:  jamonathin [ Tue May 03, 2005 8:20 am ]
Post subject: 

This is all I can think of at the moment, something else may come to me, or someone else, but this method works.
Turing:

var num : array 1 .. 100 of int
for i : 2 .. 200 by 2
    num (i div 2) := i
end for
var x : int := Rand.Int (1, 50)
put num (x)

Author:  Cervantes [ Tue May 03, 2005 9:47 am ]
Post subject: 

Take a rand.int from the lower bounds you want divided by 2 to the upper bounds, divided by 2. Then multiply result by 2.

Author:  KONjbnj [ Wed May 04, 2005 8:00 am ]
Post subject: 

Thanks, I already got the random even integer number.

All I need help in is to scramble a word. Whenever I try to do it, sometimes the letters overlap with it eachother. How do I fix this?

Author:  jamonathin [ Wed May 04, 2005 8:24 am ]
Post subject: 

Post your code so we can see what you're doing wrong. Its better for you if we correct what you have then if we just tell you the answer.

Author:  KONjbnj [ Wed May 04, 2005 8:42 am ]
Post subject: 

I tried to do this so that the order of the letters won't be the same each time.

[code]var t, u, r, i, n, g: int
put "Unscramble this word!"
randint (t, 2,6)
randint (u, 2,6)
randint (r, 2,6)
randint (i, 2,6)
randint (n, 2,6)
randint (g, 2,6)
locate (2,t)
put "T"
locate (2,u)
put "U"
locate (2,r)
put "R"
locate (2,i)
put "I"
locate (2,n)
put "N"
locate (2,g)
put "G"
[/code]

But then the letters overlap each other.

Author:  jamonathin [ Thu May 05, 2005 8:36 am ]
Post subject: 

First of all, Turing has 6 letters in it, so even if you did it right, one letter would still overlap. Secondly, you're never checking if another letter has taken that spot. You just randomly choose a spot, and hope the program knows what you're thinking.

Try this out. Smile
Turing:

var word : string
put "Enter something"
get word : * %Getting the user input (with spaces : *)
var scramble : string := "" %Varing the scrambled word
var taken : array 1 .. length (word) of int %A variable to determine if a letter in the word is taken
var hold : int %Just a variable to represent that randomized number

for i : 1 .. length (word)
    taken (i) := 0 %Setting all Letters to NOT taken (0)
end for

for q : 1 .. length (word) %Repeating this for every letter in word
    loop %The loop will only exit when every letter is scrambled and taken
        randint (hold, 1, length (word)) %Randomizing the first letter for scramble
        if taken (hold) = 0 then %If the letter isn't taken . . .
            scramble += word (hold) %Adding the new letter to scramble
            taken (hold) := 1 %Making that letter 'taken'
            exit %Exiting the loop
        end if
    end loop
end for

put scramble %Displaying the new word


: