Question about "random"

So I have these character functions:
1
2
3
4
5
6
7
8
9
10
char a0 = '-', a1 = '-', a2 = '-', a3 = '-', a4 = '-', a5 = '-', a6 = '-', a7 = '-', a8 = '-', a9 = '-';
char b0 = '-', b1 = '-', b2 = '-', b3 = '-', b4 = '-', b5 = '-', b6 = '-', b7 = '-', b8 = '-', b9 = '-';
char c0 = '-', c1 = '-', c2 = '-', c3 = '-', c4 = '-', c5 = '-', c6 = '-', c7 = '-', c8 = '-', c9 = '-';
char d0 = '-', d1 = '-', d2 = '-', d3 = '-', d4 = '-', d5 = '-', d6 = '-', d7 = '-', d8 = '-', d9 = '-';
char e0 = '-', e1 = '-', e2 = '-', e3 = '-', e4 = '-', e5 = '-', e6 = '-', e7 = '-', e8 = '-', e9 = '-';
char f0 = '-', f1 = '-', f2 = '-', f3 = '-', f4 = '-', f5 = '-', f6 = '-', f7 = '-', f8 = '-', f9 = '-';
char g0 = '-', g1 = '-', g2 = '-', g3 = '-', g4 = '-', g5 = '-', g6 = '-', g7 = '-', g8 = '-', g9 = '-';
char h0 = '-', h1 = '-', h2 = '-', h3 = '-', h4 = '-', h5 = '-', h6 = '-', h7 = '-', h8 = '-', h9 = '-';
char i0 = '-', i1 = '-', i2 = '-', i3 = '-', i4 = '-', i5 = '-', i6 = '-', i7 = '-', i8 = '-', i9 = '-';
char j0 = '-', j1 = '-', j2 = '-', j3 = '-', j4 = '-', j5 = '-', j6 = '-', j7 = '-', j8 = '-', j9 = '-';

and I want to another function to randomly pick one of them, how can I do this?
Thanks in advance
char randomMinus='-';

You also need to read up on arrays before you continue.
char randomMinus='-';

You also need to read up on arrays before you continue.

I also need this to work if they have other values than '-'
Yeah, you need to look up arrays and replace those one hundred variables with a single array.
Then choosing a random element will be easy.
Last edited on
Unfortunately, it would be VERY difficult to randomly select one of these because all of these chars are in the memory in different places.

My suggestion is to do the following instead:
char Reference[10][10] = {{'-'}};
This will create a single variable with 100 elements that can be referenced very easily. All of the elements will be initialized with a dash. To randomly select an element:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdlib.h> // Used for srand() and rand()
#include <time.h> // Used for time() which seeds the srand()

int main ()
{
  srand ( time(NULL) ); //Seed the random number generator with a different number each time.

  char Reference[10][10] = {{'-'}}; // Create the 2-d array
  int x = rand()%10; // Choose an X value between 0~9
  int y = rand()%10; // Choose a Y value between 0~9

  Reference[x][y]; //this is your random element,  write something to it, or use it at will.

  return 0;
}


If 2-d arrays aren't your thing or are unnecessary, make it a long 1-d array which would be useful if you are just learning about arrays.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdlib.h> // Used for srand() and rand()
#include <time.h> // Used for time() which seeds the srand()

int main ()
{
  srand ( time(NULL) );

  char Reference[100] = {'-'}; // Create the 1-d array
  int i = rand()%100; // Choose an i value between 0~99

  Reference[i]; //this is your random element,  write something to it, or use it at will.

  return 0;
}

Last edited on

So, say I would do this:
1
2
3
4
5
6
7
8
9
10
char array[9][9] {	{-,-,-,-,-,-,-,-,-,-},
			{-,-,-,-,-,-,-,-,-,-},
			{-,-,-,-,-,-,-,-,-,-},
			{-,-,-,-,-,-,-,-,-,-},
			{-,-,-,-,-,-,-,-,-,-},
			{-,-,-,-,-,-,-,-,-,-},
			{-,-,-,-,-,-,-,-,-,-},
			{-,-,-,-,-,-,-,-,-,-},
			{-,-,-,-,-,-,-,-,-,-},
			{-,-,-,-,-,-,-,-,-,-}};

I could access what in my first post would be a0 with:
array[0][0]

And isn't there a way to get the desired result without the array?

EDIT: Thanks Stewbond, I'll try that if it is that much easier than when I use a single variable for every one of them
Last edited on
If you already have a program written which uses all of those individual chars, then it IS possible to access them by random, but you still need to create an array like the one I created above. I'm still not great with pointers, but I think the following would work:

1
2
3
4
5
6
7
8
9
10
11
#include <stdlib.h> // Used for srand() and rand()
#include <time.h> // Used for time() which seeds the srand()
...
srand ( time(NULL) );

//This sets each element of the array to the ADDRESS of each label you made
char Reference[100] = {&a0 ,&a1,...,&j8,&j9}; // Create the 1-d array (replace ... with the other 96 variables)
int i = rand()%100; // Choose an i value between 0~99

//This returns the VALUE at each address.
*Reference[i]; //this is your random element,  write something to it, or use it at will. 
Last edited on
When I use srand ( time(NULL) );
I get the error message:
expected `,' or `;' before '(' token


EDIT: And
expected constructor, destructor, or type conversion before '(' token 
Last edited on
For your error message, look at what is ahead of srand. It means that you probably haven't entered a condition after an if statement or while loop or something. Also check that all of the {s have matching }s ahead of srand.

Note, srand only needs to be set once in a program. It is probably best to do it at the start of your main instead of in a loop to ensure that you don't get some non-random behavior.

Oh, and in response to your post above (that I didn't see), here are a few tips:
1. array[9][9] has a 9X9 grid (81 elements) and you can only access elements 0-8 (9 elements total) in each axis. You should make it array[10][10] to ensure you have a 10x10 grid (100 elements) and you can access elements 0-9 (10 entries) in each axis.
2. array[10][10] = {{'-'}}; is a shorthand way of filling all elements with the dash character, you don't have to type it manually like you did.
Topic archived. No new replies allowed.