delphi programming forums mysql charset mget recursive synonimos
free ventrilo servers hosting cs javascript delay python find in list
Back Forum New
abstract:

while (array(4) = array(3)) or (array(4) = array(2)) or (array(4) = array(1))
        array(4)=Int((x) * Rnd + 1)
wend
But, in this case, if x is a big number (like 100), the code will be very big too...
Any idea?
Thanks in advance
Ricardo http://bbs.prog365.com/images/sites/biggrin.gif


I need a array with x numbers (1 to x) mixed. Like this:
Using 4 numbers (1 to 4), i need:
array(1) = 3
array(2) = 2
array(3) = 4
array(4) = 1
This is random. I make this:
Code:
  1. x = 4
  2. randomize
  3. array(1)=Int((x) * Rnd + 1)
  4. array(2)=Int((x) * Rnd + 1)
  5. while array(2) = array(1)
  6.         array(2)=Int((x) * Rnd + 1)
  7. wend
  8. array(3)=Int((x) * Rnd + 1)
  9. while (array(3) = array(2)) or (array(3) = array(1))
  10.         array(3)=Int((x) * Rnd + 1)
  11. wend
  12. array(4)=Int((x) * Rnd + 1)
  13. while (array(4) = array(3)) or (array(4) = array(2)) or (array(4) = array(1))
  14.         array(4)=Int((x) * Rnd + 1)
  15. wend
Copy Code
But, in this case, if x is a big number (like 100), the code will be very big too...
Any idea?
Thanks in advance
Ricardo

TOP

lets see if i remember by bubble sort from my programming classes in school
PHP Code:
function
BubbleSort
(
arr
)
  for
i
=
0 to uBound
(
arr
)
     for
a
=
i to Ubound
(
arr
)
        if
arr
(
a
) <
arr
(
i
)
then
'
swap values
         tmp = arry(i)
         arr(i) = arr(a)
         arr(a) = tmp
       end if
     next
  next
BubbleSort = arr
end function
I just tested it and it works. if you do a search on google for bubble sort it will tell you how it works. Basicall it finds the lowest value and puts it in the 1st index position, then the second lowest and puts it in the second index position and so on. This is a pretty effective way to sort an array and pretty common.

TOP

Sorry, I didn't express myself well.
I want a array with shuffled numbers, and not ordened numbers. The array contains numbers, 1 to x, ordered randomly (each time the order is different), like in my code (above).
Thank you
Regards,
Ricardo



while (array(4) = array(3)) or (array(4) = array(2)) or (array(4) = array(1))
        array(4)=Int((x) * Rnd + 1)
wend
But, in this case, if x is a big number (like 100), the code will be very big too...
Any idea?
Thanks in advance
Ricardo http://bbs.prog365.com/images/sites/biggrin.gif

TOP

Back Forum