

-----------------------------------

Fashoomp

Mon May 21, 2007 5:00 pm


Rand.Int Replication

-----------------------------------

how can i use randint, so it gives my a random pattern of numbers, so it does not give me the same value twice. I want all the values between 1 and 10, given to me, in a random order.


-----------------------------------

Clayton

Mon May 21, 2007 5:09 pm


RE:Rand.Int Replication

-----------------------------------

you'll have to loop until you get all of the numbers. Now, you'll need to have some sort of array to hold all of the numbers that you've gotten. Just compare that array of numbers to the number you get from Rand.Int(), and if that number is not in the array, display it, and then add that number to the array.


-----------------------------------

Saad

Mon May 21, 2007 5:19 pm


Re: Rand.Int Replication

-----------------------------------

This is the fastest way i could come up with.

var number : array 1 .. 10 of int
var repeted : boolean := false
var minnum : int := 1  
var maxnum : int := 10  % Then random number range must be greater or equal then the upper if number or else it will never stop

for i : 1 .. upper (number)
    number (i) := Rand.Int (minnum, maxnum)
    if (i > 1) then
        loop
            repeted := false
            for b : 1 .. i - 1
                if (number (i) = number (b)) then
                    number (i) := Rand.Int (minnum, maxnum)
                    repeted := true
                end if
            end for
            exit when not repeted
        end loop
    end if
end for


for i : 1 .. upper (number)
    put number (i)
end for

Edit: Fixed some spelling mistakes  :wink:


-----------------------------------

Carey

Tue May 22, 2007 9:11 am


Re: Rand.Int Replication

-----------------------------------

Here is a shorter and more efficiant method.


var numbers : array 1 .. 10 of int %holds numbers

var i:= lower (numbers)-1 %-1 is to counteract the i += 1 at the start
loop
    i +=1
    numbers (i) := Rand.Int (lower (numbers), upper (numbers)) %choses a random #
    for j : lower (numbers) .. i - 1 %for every # chosen already not including itself
        if numbers (i) = numbers (j) then %if the # has already been picked
            i -= 1  %decrease count to stay at the same array spot
            exit    %exit to the lop that pickes another random #
        end if
    end for
    exit when i = upper(numbers) %exit when done
end loop

%TEST
for k : 1 .. upper (numbers)
    put numbers (k)
end for



-----------------------------------

Saad

Tue May 22, 2007 2:17 pm


Re: Rand.Int Replication

-----------------------------------

Is it me or does Rand.Int use system time to generate a random number, since if you ran the prog 10 times in .5 sec all the random values would be the same  :?:


-----------------------------------

DIIST

Tue May 22, 2007 3:15 pm


Re: Rand.Int Replication

-----------------------------------

A recursive solution: 
procedure random (l, h : int)
    if not (l > h) then
        var m : int := Rand.Int (l, h)
        put m
        random (l, m - 1)
        random (m + 1, h)
    end if
end random

randomize
random (1, 10)

Not the best but it works for the most part :wink: .

Is it me or does Rand.Int use system time to generate a random number, since if you ran the prog 10 times in .5 sec all the random values would be the same  :?:
Yes i do believe system time is used as the random seed generator. But you can easily reset it by doing randomize (I think). Its all machine dependant. Some machines, regardless of what you do may still spit out the same set of value again and again at each runtime.


-----------------------------------

Cervantes

Tue May 22, 2007 5:56 pm


RE:Rand.Int Replication

-----------------------------------

I'm pretty sure randomize is obsolete in the newer versions of Turing. Yes, the system time is used. Theoretically, I think it uses milliseconds, so you probably shouldn't be seeing any patterns. However, practically, I've seen them too.

you'll have to loop until you get all of the numbers. Now, you'll need to have some sort of array to hold all of the numbers that you've gotten. Just compare that array of numbers to the number you get from Rand.Int(), and if that number is not in the array, display it, and then add that number to the array.
I can't believe you just said this. This scheme can take an unbounded amount of time. This really sucks, when you compare it to the better solution, which takes exactly 10 iterations, where each iteration requires a little search (could easily be done in log n time, if you do a binary search).


-----------------------------------

Clayton

Tue May 22, 2007 6:25 pm


RE:Rand.Int Replication

-----------------------------------

I'm not sure if you're thinking about this the same way I was thinking about it when I wrote it. Perhaps I just didn't convey my message clearly. For that, I apologize.

