Random Number Generation

Can anyone tell me how to generate random numbers between 0-1.  I have to intial a 2d matrix with these values...
d_caesarAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dviguyCommented:
This is an easy one.

First, your application must "seed" the random number generator one time before using it.  Use the srand() function to do this, i.e.

// Seed with current system time
srand((unsigned)time(NULL));

From then on, simply call this function to get a random number between 0 and 1.

double GetRand()
{
   return (double)rand() / RAND_MAX;
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
d_caesarAuthor Commented:
Thx for the prompt reply(I know its easy, it just din't striked me!)
How about intializing a 2D matrix???
0
dviguyCommented:
// This function will initialize a
// 2D matrix with random numbers
// between zero and one

#define NUM_COLS 10
#define NUM_ROWS 10
double matrix[NUM_COLS][NUM_ROWS];

void Init2DMatrix()
{
   int i, j;
   for (i = 0; i < NUM_COLS; i++)
      for (j = 0; j < NUM_ROWS; j++)
         matrix[i][j] = GetRand();
}

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

d_caesarAuthor Commented:
Thats good! I am fine tuning the rand function so that the numbers vary coz with your programme atleast 5 random numbers are the same.
Any Suggestions. I am trying to use the SYSTEMTIME and GetSystemTime struct with wMilliseconds for the seed but seems like it doesn't behaves well!
0
dviguyCommented:
You only want to call the srand() function one time (usually at program startup).  Each time you call it will cause the random number generator to be reset.  Calling it with the same seed value will cause the exact same series of random numbers to be generated.

That said, the srand() function needs an unsigned integer for the seed value.  How you generate this seed value is inmaterial, although typically it is done using time or date/time.  Sounds like you are calling srand() several times in your application and that is the cause of your "same number" problem.
0
d_caesarAuthor Commented:
You are right I am calling inside the loop. Ok I will correct that.
Let you know..
0
d_caesarAuthor Commented:
This is what I am doing !!gives me similar random numbers

float Randome()
{
      
      float value=0.0;
      SYSTEMTIME sysTime;
      GetSystemTime(&sysTime);
      srand((unsigned)sysTime.wMilliseconds);
      for(int i=0;i<10;i++)
      {
            float value =(float)rand() / RAND_MAX;
            return value;

      }

      return value;
      

      
}


int main()
{

      float k,q;
      int row,col;
      float Matrix[100][100];
      for(int c=0;c<6;c++)
      printf("%8d",c);
      printf("\n");
            printf("====================================\n");

      
             for(int i=0;i<6;i++)
         {
         row=i+1;
         printf("%2d|",row);
       for(int j=0;j<6;j++)
      {
         k= Randome();
        q= Randome();
        Matrix[i][j]=k,q;
        printf("%8f ",Matrix[i][j]);
      }
      cout<<endl;
          }
      
      return 0;
}
0
dviguyCommented:
Move these lines from your Randome() function and put them at the beggining of Main().  You should only call srand() ONE time each time your application runs, not dozens of times.  Also, by using the wMilliseconds value you will limit your program to 1000 different series of random numbers.  You should use a number that ranges from 0 to 0xFFFFFFFF.  time(NULL) returns such a value.

SYSTEMTIME sysTime;
GetSystemTime(&sysTime);
srand((unsigned)sysTime.wMilliseconds);

This is the most common problem folks have with random numbers - knowing how to "seed" them correctly.
0
d_caesarAuthor Commented:
Thx I will do that, I have posted another question can U please look that if you have time....


Thx
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.