hi i want to know how exactly can i use time.h and rand() if i want to print every time a pair of numbers but the same pair will be printed only once for example:
2 2
2 3
1 4
3 5
2 2<----- not to happen(instead get a new one)
1 4<-----not to happen (instead get a new one)

pliz help me with a code....

What code do you have so far?

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


int main ()
{

int map1[20],map2[20],l,k,i, j;
/* initialize random generator */
for (k=1;k<=20;k++){
    
rand();
i=rand() % 10;
rand();
j=rand() % 10;
map1[k-1]=i;
map2[k-1]=j;

    
printf("%d%d\n", i, j);}

system("pause >nul");
return 0;
}

this is so far...i guess that i have to make some arrays like map1,map2 that will help me.....can u transform my code??so that i can understand how can i achieve my goal?

Your code could potentially generate from
0 0
to
9 9

That's 100 possibly combinations right?

So your "I've seen this before" test needs that much space to remember what has gone before.

i don't exaclty get it....u mean tha t i have to create an array right....but what should i do exaclty?can u show me an example..?plizzz

Google for srand

You want some thing of this sort ...
After you have got random numbers in i and j

if(i==j)
{
// Do nothing
}
else
{
   // Execute the loop below
}

for(z=0;z<20;i++)
{
    if( (map1[z]==i)&&(map2[z]==j))
   {
        //Do nothing
   }
     else 
     {
         // Insert into the array
      }
}

i dont thing u get it all i want to do is to have one by one pairs e.g
1-2
3-2
1-5
3-8
8-4
etc. but i want to ensure that there will not be created the same pair twice.....e.g
1-2
4-5
1-2<----nooo
8-8
etx

can u transform my code?i am kinda lost....and which way is better?with time.h library or just with rand()?

we're not going to fix your code for you dude. you're going to do that yourself.

(1) keep track of each number pair you generate by setting the appropriate element in a 10x10 array.

(2) each time you generate a new pair, check and see if your 10x10 array has marked that pair as having already occurred.

(3) if it's already occurred, generate a new pair and repeat #2

(4) if it hasnt already occurred flag the appropriate variable in the array and repeat #1


continue to do this until you have as many numbers as you need.

ok but how exactly can i check the 10X10 array?just run over it?with for loops?

ok but how exactly can i check the 10X10 array?just run over it?with for loops?

10 * 10 array has 100 location. But I guess you are just going to be using 20 . So be careful where you store the numbers and remember to check only those locations
PS. I would rather use 2 arrays of 10 integers than a 10* 10 matrix.... I think it will be more simpler

I think this is an easier way:

1) Generate every 2 digit number, and put it into an int array[100]. Just one dimension is needed. Note that you will have only 89 2 digits numbers, in the range 10 - 99.

2) Now your program selects a rand() % N + 10 //(10 to 89)
N = 90 to start with.

Each time your program chooses a random number from the array (by index), you will move the highest random number element, and put it into the element that was just chosen.

So if your random number was 23, then you move whatever value is in [99], into [23]. N now becomes N--, since you have used up one of the random numbers.

That's pretty slick and easier to do than to describe, almost,

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.