Solved

i want to generate such number of random numbers as i want, but seems like it dont work properly

Posted on 2004-08-20
8
197 Views
Last Modified: 2010-04-15
here is the sample of my program, the M is the number of random numbers i want
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define X 100
#define M 10

main()
{
      int m[X][X];
      int i, j, s = 0, ran;
      
      srand(time(NULL));

      for(i = 0; i < X + 1; i++)
      {
            for(j = 0; j < X + 1; j++)
            {
                  printf("m[%d][%d]\n", i, j);
                  while(s < M)
                  {
                        ran = rand() % 10 + 1;      
                        if(ran == 1)                  
                        {
                              m[i][j] = 1;
                              s = s + 1;
                              printf("m[%d][%d] = %d\n", i, j, m[i][j]);      
                        }            
                  }
            }
      }

}
0
Comment
Question by:quinnyoom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
8 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 11851645
You need to initialize the array m[] to whatever value you want for untouched cells.

Your for loops are going one step too far I think.


Also, your logic looks faulty,

for each cell, you're looping until you get 10 times a random number == 1, then setting that cell to 1.   Doesnt make much sense.

What do you really want to do?


0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11851846
>>    for(i = 0; i < X + 1; i++)
>>     {
>>          for(j = 0; j < X + 1; j++)

Your loop limit is too high! You should use:

    for(i = 0; i < X; i++)
     {
          for(j = 0; j < X; j++)

You have forgotten to intitialise m[i][j] so I would also suggest:

    for(i = 0; i < X; i++)
     {
          for(j = 0; j < X; j++)
          {
             m[i][j] = 0;  // *** Add this! (as grg99 suggested).

Otherwise, this looks OK.

m[i][j] will contain a '1' if any of the M random numbers (between 1 and 10) generated was equal to '1'.

I hope this isnt homework or coursework because I am not allowed to give you code in this case.

Paul
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11851876
Err!!! I just noticed another one! You need to reset 's' to zero each time around or use a for loop.

Change:

>>               while(s < M)

to

               for(s = 0; s < M; s++)

and remove the:

 >>                       s = s + 1;
 
Thinking about it, this doesnt seem like a terribly useful process, I'm thinking more and more that this is homework. Sorry sunny, my mistake.

Paul
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:PaulCaswell
ID: 11851899
If you change:

>>                         m[i][j] = 1;

to

                         m[i][j] += 1;

you get a much more meaningful array.

Paul
0
 
LVL 22

Expert Comment

by:grg99
ID: 11852563
The inner while loop is definitely "funny", the logic right now has it loop indefinitely, until the random number generator has given out ten "1"'s.

 If you move the s = s + 1 to outside the if(), then you get a somewhat more useful loop, it sets each element to "1" if and only if yo get a "1" in 10 tries.   Still not terribly interesting, but better than before.

0
 

Author Comment

by:quinnyoom
ID: 11852571
its not a homework ^^ just i m thinking, normal
for(i = 0; i < X; i++)
{
          for(j = 0; j < X; j++)
          {
                    ran = rand() % 10 + 1;    
                    if(ran == 1)                
                    {
                         m[i][j] = 1;
                         printf("m[%d][%d] = %d\n", i, j, m[i][j]);    
                    }          
               }
          }
}
we cant figure out how much m[x][y] = 1 will the program generate
and now i want that i can set M m[x][y] = 1(= i want to generate M points that mark with "1", but must place random within the 100 x 100) as i want, like below?
 
for(i = 0; i < X; i++)
{
          for(j = 0; j < X; j++)
          {
               while(s < M)
               {
                    ran = rand() % 10 + 1;    
                    if(ran == 1)                
                    {
                         m[i][j] = 1;
                         s = s + 1;
                         printf("m[%d][%d] = %d\n", i, j, m[i][j]);    
                    }          
               }
          }
}
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 500 total points
ID: 11852880
>>we cant figure out how much m[x][y] = 1 will the program generate

rand() % 10 + 1 should generate numbers from 1 to 10 at random so one in ten should be set to '1' the rest will be left alone. Unfortunately, because you are not clearing the array first, all the rest may be random values. If you use:

                   if(ran == 1)                
                    {
                         m[i][j] = 1;
                         s = s + 1;
                         printf("m[%d][%d] = %d\n", i, j, m[i][j]);    
                    }
                    else
                    {
                         m[i][j] = 0;
                    }

you will get a more reliable result.

>>and now i want that i can set M m[x][y] = 1(= i want to generate M points that mark with "1", but must place random within the 100 x 100) as i want, like below?
 
If this means what I think it means then perhaps:

>>               while(s < M)

should become

               if(s < M)

but this would only fill the first ten that would normall be set.

If you really want ten random entries set to '1' then use something like:

// Clear the array.
for(i = 0; i < X; i++)
          for(j = 0; j < X; j++)
            m[i][j] = 0;
// None set.
s = 0;
// Set ten random ones to 1.
while ( s < M )
{
 // Pick a random location.
 i = rand()%X;
 j = rand()%X;
 // Has it already been set?
 if ( m[i][j] == 0 )
 {
  // No! Set it.
  m[i][j] = 1;
  // And remember to count it.
  s += 1;
 }
}

Paul
0
 

Author Comment

by:quinnyoom
ID: 11858500
thx for the advice, i can figure out how the random work well now
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
why debugging a macro i s difficult 10 45
Best UNIX-compatible free C compiler for Windows or Mac 6 287
delete-remove 14 116
Please explain C language error codes ? 5 50
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question