Rand.Int Replication
Author |
Message |
Fashoomp
|
Posted: Mon May 21, 2007 5:00 pm Post subject: 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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Clayton
|
Posted: Mon May 21, 2007 5:09 pm Post subject: 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
|
Posted: Mon May 21, 2007 5:19 pm Post subject: Re: Rand.Int Replication |
|
|
This is the fastest way i could come up with.
code: | 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 |
|
|
|
|
|
Carey
|
Posted: Tue May 22, 2007 9:11 am Post subject: Re: Rand.Int Replication |
|
|
Here is a shorter and more efficiant method.
Turing: |
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
|
Posted: Tue May 22, 2007 2:17 pm Post subject: 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
|
Posted: Tue May 22, 2007 3:15 pm Post subject: Re: Rand.Int Replication |
|
|
A recursive solution: Turing: |
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 .
a100 @ Tue May 22, 2007 2:17 pm wrote: 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
|
Posted: Tue May 22, 2007 5:56 pm Post subject: 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.
Clayton wrote: 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
|
Posted: Tue May 22, 2007 6:25 pm Post subject: 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. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|