[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

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...
0
d_caesar
Asked:
d_caesar
  • 5
  • 4
1 Solution
 
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
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now