こちらの環境が windows/eclipse で c の処理系もテキトウなもので, 動作するか分かりませんが...(コマンドプロンプトで動かしました.)
何をシャッフルするかで2パターン作ってみました.
1 はポインタをシャッフルします.
2 はインデックスをシャッフルします.
結果は同じです.
c
1 # include <stdio.h>
2 # include <stdlib.h>
3 # include <time.h>
4 # include <conio.h>
5
6 # define COLUMNS 4
7 # define ROWS 4
8
9 int values [ ROWS ] [ COLUMNS ] = { { 1 , 2 , 3 , 4 } , { 4 , 2 , 5 , 7 } , { 8 , 1 , 9 , 5 } , { 9 , 2 , 3 , 1 } } ;
10
11 void doProcess ( int v [ ] ) {
12 printf ( "%d,%d,%d,%d\n" , v [ 0 ] , v [ 1 ] , v [ 2 ] , v [ 3 ] ) ;
13 }
14
15 void suffle1 ( int * pointers [ ] ) {
16 srand ( ( unsigned int ) time ( 0 ) ) ;
17 for ( int i = ROWS - 1 ; i > 1 ; i -- ) {
18 int j = rand ( ) % i ;
19 int * p = pointers [ i ] ; pointers [ i ] = pointers [ j ] ; pointers [ j ] = p ;
20 }
21 }
22 void pattern1 ( ) {
23 int * pointers [ ROWS ] ;
24 for ( int i = 0 ; i < ROWS ; i ++ ) pointers [ i ] = values [ i ] ;
25
26 printf ( "pattern 1\n" ) ;
27 while ( 1 ) {
28 char c = getch ( ) ;
29 printf ( "(%02x)\n" , c ) ;
30 if ( c == 0x1b ) break ; //ESC
31 if ( c == 0x0d || c == 0x0a ) {
32 printf ( "suffle.\n" ) ;
33 suffle1 ( pointers ) ;
34 } else if ( '1' <= c && c <= '4' ) {
35 int key = c - '1' ;
36 doProcess ( pointers [ key ] ) ;
37 }
38 }
39 }
40
41 void suffle2 ( int array [ ] ) {
42 srand ( ( unsigned int ) time ( 0 ) ) ;
43 for ( int i = ROWS - 1 ; i > 1 ; i -- ) {
44 int j = rand ( ) % i ;
45 int v = array [ i ] ; array [ i ] = array [ j ] ; array [ j ] = v ;
46 }
47 }
48 void pattern2 ( ) {
49 int indexes [ ROWS ] ;
50 for ( int i = 0 ; i < ROWS ; i ++ ) indexes [ i ] = i ;
51
52 printf ( "pattern 2\n" ) ;
53 while ( 1 ) {
54 char c = getch ( ) ;
55 printf ( "(%02x)\n" , c ) ;
56 if ( c == 0x1b ) break ; //ESC
57 if ( c == 0x0d || c == 0x0a ) {
58 printf ( "suffle.\n" ) ;
59 suffle2 ( indexes ) ;
60 } else if ( '1' <= c && c <= '4' ) {
61 int key = c - '1' ;
62 doProcess ( values [ indexes [ key ] ] ) ;
63 }
64 }
65 }
66
67 int main ( int argc , char * argv [ ] ) {
68 if ( argc == 1 ) pattern1 ( ) ;
69 else pattern2 ( ) ;
70 }
#追加
ざっくりと構造体化しました.
x,y の扱いがはっきりしていませんのでそこはテキトウです.
c
1 # include <stdio.h>
2 # include <stdlib.h>
3 # include <time.h>
4 # include <conio.h>
5
6 # define STIMULI_ROWS 5
7
8 typedef struct {
9 double freqv1 ;
10 double freqv2 ;
11 double x ;
12 double y ;
13 double ampv1 ;
14 double ampv2 ;
15 double ampe1 ;
16 double ampe2 ;
17 } STIMULI ;
18
19 STIMULI initValues [ STIMULI_ROWS ] = {
20 { 1.5 , 1.0 , 1.0 , 1.0 , 0.655 , 0 , 0 , 0 } ,
21 { 2.5 , 1.0 , 1.0 , 1.0 , 1.275 , 0 , 0 , 0 } ,
22 { 1.3 , 1.9 , 5.33 , 5.0 , 0.301 , 0 , 0 , 0 } ,
23 { 1.3 , 2.5 , 3.33 , 5.33 , 0.393 , 0 , 0 , 0 } ,
24 { 1.9 , 2.5 , 4.0 , 2.67 , 0.327 , 0 , 0 , 0 } ,
25 } ;
26
27 void doProcess ( STIMULI * v ) {
28 printf ( "%4.3f %4.3f %4.3f %4.3f \n" , v -> ampv1 , v -> ampv2 , v -> ampe1 , v -> ampe2 ) ;
29 }
30
31 void initStimult ( STIMULI v [ ] ) {
32 for ( int i = 0 ; i < STIMULI_ROWS ; i ++ ) v [ i ] = initValues [ i ] ;
33 }
34 void calcStimult ( STIMULI v [ ] /*, double x, double y*/ ) {
35 for ( int i = 0 ; i < STIMULI_ROWS ; i ++ ) {
36 /*stimuli[i].x = x;*/
37 /*stimuli[i].y = y;*/
38 v [ i ] . ampv2 = i < 2 ? 0 : ( v [ i ] . x * v [ i ] . ampv1 ) ;
39 v [ i ] . ampe2 = v [ i ] . y * v [ i ] . ampe1 ;
40 }
41 }
42
43 void suffleStimult ( STIMULI stimuli [ ] ) {
44 srand ( ( unsigned int ) time ( 0 ) ) ;
45 for ( int i = STIMULI_ROWS - 1 ; i > 1 ; i -- ) {
46 int j = rand ( ) % i ;
47 STIMULI v = stimuli [ i ] ; stimuli [ i ] = stimuli [ j ] ; stimuli [ j ] = v ;
48 }
49 }
50
51 void pattern1 ( ) {
52 STIMULI v [ STIMULI_ROWS ] ;
53 initStimult ( v ) ;
54 calcStimult ( v /*,x,y*/ ) ;
55
56 printf ( "pattern 1\n" ) ;
57 while ( 1 ) {
58 char c = getch ( ) ;
59 printf ( "(%02x)\n" , c ) ;
60 if ( c == 0x1b ) break ; //ESC
61 if ( c == 0x0d || c == 0x0a ) {
62 calcStimult ( v /*,x,y*/ ) ; //必要?
63 printf ( "suffle.\n" ) ;
64 suffleStimult ( v ) ;
65 } else if ( '1' <= c && c <= ( '0' + STIMULI_ROWS ) ) {
66 int key = c - '1' ;
67 doProcess ( & v [ key ] ) ;
68 }
69 }
70 }
71
72 int main ( int argc , char * argv [ ] ) {
73 pattern1 ( ) ;
74 }