
-----------------------------------
KONjbnj
Tue May 03, 2005 7:44 am

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.

Nevermind, I figured out the even number thing. I used:



-----------------------------------
jamonathin
Tue May 03, 2005 8:20 am


-----------------------------------
This is all I can think of at the moment, something else may come to me, or someone else, but this method works.

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)


-----------------------------------
Cervantes
Tue May 03, 2005 9:47 am


-----------------------------------
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.

-----------------------------------
KONjbnj
Wed May 04, 2005 8:00 am


-----------------------------------
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?

-----------------------------------
jamonathin
Wed May 04, 2005 8:24 am


-----------------------------------
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.

-----------------------------------
KONjbnj
Wed May 04, 2005 8:42 am


-----------------------------------
I tried to do this so that the order of the letters won't be the same each time.



But then the letters overlap each other.

-----------------------------------
jamonathin
Thu May 05, 2005 8:36 am


-----------------------------------
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.  :) 

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

