It is currently Fri Jul 24, 2026 10:14 am


All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: How to shuffle the values in a vector array ?
PostPosted: Mon Jun 01, 2015 11:35 am  (#1) 
Offline
GimpChat Member
User avatar

Joined: Dec 26, 2014
Posts: 205
#( 0 0 0 0 1 1 1 1 2 2 2 2 )

I’ve created this small test vector array in the SFC and filled it with an equal number of zero’s, one’s and two’s, these are to be used as conditional flags in a script

What I need to do next is shuffle them into a random order, but I can’t work out how to do it, any advice would be appreciated

Thanks for looking


Share on Facebook Share on Twitter Share on Orkut Share on Digg Share on MySpace Share on Delicious Share on Technorati
Top
 Post subject: Re: How to shuffle the values in a vector array ?
PostPosted: Mon Jun 01, 2015 11:52 am  (#2) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4513
Location: Canada
this is probably a total hack but you could do something like this
(define v #( 0 0 0 0 1 1 1 1 2 2 2 2 ))
(srand (car (gettimeofday)))
(define a 0)
(define b 0)
(define temp 0)
(define swaps 12) ; number of random swaps
(while (> swaps 0)
    (set! a (rand (vector-length v)))
    (set! b (rand (vector-length v)))
    (set! temp (aref v a))
    (aset v a (aref v b))
    (aset v b temp)
    (set! swaps (- swaps 1))
)







_________________
TinT


Top
 Post subject: Re: How to shuffle the values in a vector array ?
PostPosted: Mon Jun 01, 2015 12:42 pm  (#3) 
Offline
GimpChat Member
User avatar

Joined: Dec 26, 2014
Posts: 205
trandoductin wrote:
this is probably a total hack but you could do something like this
(define v #( 0 0 0 0 1 1 1 1 2 2 2 2 ))
(srand (car (gettimeofday)))
(define a 0)
(define b 0)
(define temp 0)
(define swaps 12) ; number of random swaps
(while (> swaps 0)
    (set! a (rand (vector-length v)))
    (set! b (rand (vector-length v)))
    (set! temp (aref v a))
    (aset v a (aref v b))
    (aset v b temp)
    (set! swaps (- swaps 1))
)







Thanks for the quick reply, it looks interesting. I need to spend some time now analysing the script to follow the logic and then hope to adapt it to run in my script.

Thanks again

Steve


Top
 Post subject: Re: How to shuffle the values in a vector array ?
PostPosted: Tue Jun 02, 2015 6:45 am  (#4) 
Offline
GimpChat Member
User avatar

Joined: Dec 26, 2014
Posts: 205
trandoductin wrote:
this is probably a total hack but you could do something like this
(define v #( 0 0 0 0 1 1 1 1 2 2 2 2 ))
(srand (car (gettimeofday)))
(define a 0)
(define b 0)
(define temp 0)
(define swaps 12) ; number of random swaps
(while (> swaps 0)
    (set! a (rand (vector-length v)))
    (set! b (rand (vector-length v)))
    (set! temp (aref v a))
    (aset v a (aref v b))
    (aset v b temp)
    (set! swaps (- swaps 1))
)







I see how it works by swapping the values of two randomly chosen cells, and I take it, referring to it being a total hack is that there is no guarantee all locations are changed, but this might actually work in my favour so I made the variable (swaps) a value to be input. The only changes I made was aset/aref to vector-set/ref

My original script generated a sloped image that produced a bad join when polar coordinates were used

Your hack aids in generating a level image (image 2)

Which gives a good join when polar coordinates are used (image 3)

Thanks again, it would have taken me a long time to work that code out myself if ever

Attachment:
1.jpg
1.jpg [ 26.03 KiB | Viewed 2746 times ]

Attachment:
File comment: image 2
2.jpg
2.jpg [ 105.47 KiB | Viewed 2746 times ]

Attachment:
File comment: image 3
3.jpg
3.jpg [ 91.94 KiB | Viewed 2746 times ]


Top
 Post subject: Re: How to shuffle the values in a vector array ?
PostPosted: Tue Jun 02, 2015 9:33 am  (#5) 
Offline
Script Coder
User avatar

Joined: May 07, 2014
Posts: 4513
Location: Canada
glad it helped.
Those are some ultra cool images btw.

_________________
TinT


Top
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]



* Login  



Powered by phpBB3 © phpBB Group