Hi, i have this letters, A,R,V,G,M,N,B. And from those i need to get 4 of them randomly, bit i dont know how to do it, please help! :D

first consider:

char letters[7] = {"ARVGMNB"};

for (i=0; i<7; i++)
   printf("letter # %d = %c\n", letters[i]);

then consider:

int number = rand() % 7;
print ("random number from 0-6 is :  %d\n", number);

now somehow you need put em together in your program.

.

Ok then, this is my code, but i keep getting the same results, each time i run it i get B V G N,

main ()
{
  char letters[6];
  int max, min;
  int i, comp;
  letters[0]=65;//Ascii
  letters[1]=66;
  letters[2]=71;
  lettesr[3]=77;
  letters[4]=78;
  letters[5]=86;
  min=0;
  max=5;
  i=0;
  comp=-1;
  while(i<4)
    {
      int number = rand()%6;
      if(comp!=number)
	{
	  printf("Random Number: %c\n", letters[number]);
	  i++;
	}
      comp=number;
    }
}

#include <time.h> and you should already have stdlib because of rand(). At the top at some point before line 18 call srand((unsigned)time(0)); to seed the random number generator using the current time (and converts that value to one acceptable to srand).

A couple of little things: main() should explicitly return an int, so mark it as int main(void) and put a return 0; at the end of your code (before the last brace of main). Also, in lines 6-11 you can use the characters (e.g., 'A' ) to make things easier to read so you won't have to remember.

Thanks a lot man, i got it now

I just got another doubt, it works nicely but the first letter it generates is R, i know its abbout the seed or something, how can i fix that

srand( (unsigned) time( 0 ) );
 while(i<4)
    {
      
      int number = rand()%7;
      if(letters[number] != mem[0] && letters[number] != mem[1] && letters[number] != mem[2] && letters[number] != mem[3] )
      {
	printf("random letter %d: %c\n",number, letters[number]);
	mem[j] = letters[number];
	i++;
	j++;
	
      }

It always generates R as the first letter, or what is the situation?

It always generates R as the first letter, or what is the situation?

Yeah, i cant't fix it :S

These

random letter 6: B
random letter 4: M
random letter 3: G
random letter 5: N

random letter 3: G
random letter 5: N
random letter 4: M
random letter 2: V

random letter 6: B
random letter 0: A
random letter 3: G
random letter 4: M

are 3 sample runs that I generated by using your post #6 and forming a main around it. Try running that section in isolation in another file (with a main around it and the variables declared). Post your complete code in case you have changed something but I'm not sure what it would be.

It just wont work :S
this is my complete code

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>

int main(void)
{
  int number;
  char letters[6];
  int mem[3];
  int i, j;
  letters[0]=65;//A
  letters[1]=66;//B
  letters[2]=71;//G
  letters[3]=77;//M
  letters[4]=78;//N
  letters[5]=82;//R
  letters[6]=86;//V
  i=0;
  j=0;
  srand( (unsigned) time(0) );
  
  while(i<4)
    {
      
      int number = rand()%7;
      if(letters[number] != mem[0] && letters[number] != mem[1] && letters[number] != mem[2] && letters[number] != mem[3] )
	{
	  printf("Random letter %d: %c\n",number, letters[number]);
	  mem[j] = letters[number];
	  i++;
	  j++;
	  
      }
      else
	i=i;
      
    }
  return 0;
}

Using your exact program:

Random letter 6: V
Random letter 3: M
Random letter 2: G
Random letter 4: N


Random letter 1: B
Random letter 0: A
Random letter 2: G
Random letter 6: V


Random letter 0: A
Random letter 5: R
Random letter 6: V
Random letter 4: N


Random letter 4: N
Random letter 0: A
Random letter 2: G
Random letter 5: R

No repro of that phenomenon on my end. Are you on any platform that's out of the ordinary?

As one of those "it's probably not but I've gotta ask" are you compiling an old version of your program by mistake?

No, im not compiling any older version, maybe the problem is i have a Mac computer haha, ill test it tomorrow in UNIX, i hope it works right.

Random letter 5: R
Random letter 2: G
Random letter 0: A
Random letter 4: N

Random letter 5: R
Random letter 3: M
Random letter 0: A
Random letter 6: V

Random letter 5: R
Random letter 0: A
Random letter 4: N
Random letter 3: M

Just for kicks try changing the 65 to 'A' etc. in lines 12-18 on your posting of your full code (like I had suggested before too). It could be a character set problem. Just another shot in the dark but writing stuff without the ASCII will guarantee it's platform independent.

Just for kicks try changing the 65 to 'A' etc. in lines 12-18 on your posting of your full code (like I had suggested before too). It could be a character set problem. Just another shot in the dark but writing stuff without the ASCII will guarantee it's platform independent.

Nope, i did it and it is the same thing, the problem is that the first number it generates is always 5, and "letters[5]=R"

Well, it was worth a shot. Anyway, hope you can get it to work on your other system. Good luck.

Since you never initialized the mem[] array, try displaying it just to see what's in it. It could be anything, including 'R'.

Since you never initialized the mem[] array, try displaying it just to see what's in it. It could be anything, including 'R'.

Random Letter 5: R
Random Letter 6: V
Random Letter 3: M
Random Letter 1: B
mem[0]=R
mem[1]=V
mem[2]=M
mem[3]=B

That's what mem[] contains

bizarre. it works correctly for me.

are you sure you're compiling the code and then using the executable that you just compiled?

sometime, if i'm not paying attention, i may compile my latest code, but use an old executable.

recompile your code, then check the timestamp on your executable or binary file before you run it. or even put an extra printf statement in there with a unique print output.

the point is to make sure that you are indeed running the code you think you are.

Random Letter 5: R
Random Letter 6: V
Random Letter 3: M
Random Letter 1: B
mem[0]=R
mem[1]=V
mem[2]=M
mem[3]=B

That's what mem[] contains

Before you use it :icon_rolleyes:

char letters[6];
  int mem[3];
  int i, j;
  letters[0]=65;//A
  letters[1]=66;//B
  letters[2]=71;//G
  letters[3]=77;//M
  letters[4]=78;//N
  letters[5]=82;//R
  letters[6]=86;//V

You are accessing the array boundary here. Change 6 to 7 in the array declaration.

commented: doh! look at how many "veteran posters" missed that! good catch :) +6
commented: Yes, excellent job +3

Oh yeah, didn't notice that. char letters[6]; can only access [0] to [5] - 6 values int mem[3]; can only access [0] to [2] - 3 values

commented: didn't notice mem[3] access. +1
commented: Excellent job too, I guess ;) +0

The problem is my Mac, i tested it today in UNIX and it worked fine, thanks everyone :)

no, the problem is not your Mac.

The fact that it happens to work on some environments (your Unix) but not others (your Mmac) doesn't mean that Mac is the problem. Mac is working correctly. your CODE is wrong.

as what thomas noticed, above, your array sizes are not defined correctly.

char letters[[B]6[/B]];
int mem[[B]3[/B]];

must be changed to

char letters[[B]7[/B]];
int mem[[B]4[/B]];

if you leave it the other way, even if it happens to work on some particular platform, the code is still wrong.

no, the problem is not your Mac.

The fact that it happens to work on some environments (your Unix) but not others (your Mmac) doesn't mean that Mac is the problem. Mac is working correctly. your CODE is wrong.

as what thomas noticed, above, your array sizes are not defined correctly.

char letters[[B]6[/B]];
int mem[[B]3[/B]];

must be changed to

char letters[[B]7[/B]];
int mem[[B]4[/B]];

if you leave it the other way, even if it happens to work on some particular platform, the code is still wrong.

I fixed that, and it didn't work either, i still had the problem, when i tested it on UNIX it worked fine, so yes the problem i believe is my Mac

commented: sometimes you just gotta tell a "veteran poster" that he's wrong ;) +6

I fixed that, and it didn't work either, i still had the problem, when i tested it on UNIX it worked fine, so yes the problem i believe is my Mac

hmm.

now that's a head scratcher.

okay, i'm going to leave this alone.

Can you try with this

The problem that you are facing could be a "known issue".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.