none
problem in Generating random numbers... (OPENMP) RRS feed

  • Question

  • Hi everybody...!

    can anyone help me in generating random numbers using openmp features...

    i tried myself(code is as shown below). but i am getting same set of random no. for every object ,.. i think some silly mistake is present in the code so can anyone let me know what it is?

     

    #include "stdafx.h"
    #include<omp.h>
    #include<iostream>
    #include<stdlib.h>
    #include<windows.h>
    #include<stdio.h>
    
    using namespace std;
    
    class Data
    {
    public:
    	Data(){}
    	int *Arr;
    	int Size;
    	void Generator()
    	{
    		srand((int) time_t(NULL));
    		for(int i=0;i<Size;i++)
    		{
    			Arr[i]=(rand()%(Size * 1000));
    		}
    	}
    };
    Data *Obj;
    int _tmain(int argc, _TCHAR* argv[])
    {
         //read no_of_threads
    	Obj = (Data *) malloc(sizeof(Data) * no_of_threads);
    	for(int i=0;i<no_of_threads;i++)
    	{
    		Obj[i].Size = 1000; //Because 1000 random no. needed
    		Obj[i].Arr = (int *) malloc(sizeof(int) * 1000);
        }
    	omp_set_num_threads(no_of_threads);
        #pragma omp parallel
        #pragma omp for private(tid)
        	for(int i=0;i<no_of_threads;i++)
        	{
        		tid = omp_get_thread_num();
                cout << "Thread id : " << tid; 
            	Obj[i].Generator();
        	}
        //.... some more code ...
        //....
    }
    
    But this kind of generating random no. is actually generating same set of random no. for every object... what is the mistake in this code?

     

    what modification needs to be done inorder to get different random no. for each object..?

    Please let me know the mistake i have done..

    what should be done inorder to achieve it...?

    plz help me..!


    - Vinay
    Monday, November 1, 2010 1:25 PM

Answers

  • Note that srand() should only be called *once* to seed
    the random number generator. You are calling it every
    time you invoke the Generator() member function. You may
    try moving srand() to your class' constructor.

    - Wayne

    • Marked as answer by Jaki Chan Monday, November 1, 2010 2:04 PM
    Monday, November 1, 2010 1:37 PM

All replies

  • Note that srand() should only be called *once* to seed
    the random number generator. You are calling it every
    time you invoke the Generator() member function. You may
    try moving srand() to your class' constructor.

    - Wayne

    • Marked as answer by Jaki Chan Monday, November 1, 2010 2:04 PM
    Monday, November 1, 2010 1:37 PM
  • jaki chan wrote:

    i tried myself(code is as shown below). but i am getting same set of  random no. for every object ,.. i think some silly mistake
    is present in the code so can anyone let me know what it is?

    srand((int) time_t(NULL));

    This is an elaborate way to write srand(0). time_t is a type, not a  function. You probably meant srand((int) time(NULL)).

    However, time() only has one second resolution. Chances are high, you'll  get the same value out of it when you call it several times in quick  succession. You need some other way of seeding.


    Igor Tandetnik

    Monday, November 1, 2010 1:50 PM
  • Thanks Wayne,...

    i did as you said,.. i.e. moved that srand() to  its constructor,..

    i am getting random no. for obj[0] different from obj[1].

    but from obj[1] to all objects i.e, obj[n] are having same random no. set... :(

    what else is problem in the code.. can you help me.. please..


    - Vinay
    Monday, November 1, 2010 1:52 PM
  • WayneAKing wrote:

    Note that srand() should only be called once to seed
    the random number generator. You are calling it every
    time you invoke the Generator() member function. You may
    try moving srand() to your class' constructor.

    srand() is per thread. Generator would get called on a different thread  than the constructor (had the construtor been called at all; the way the  OP uses malloc to allocate Obj, it doesn't).


    Igor Tandetnik

    Monday, November 1, 2010 1:53 PM
  • >(had the construtor been called at all; the way the
    >OP uses malloc to allocate Obj, it doesn't).

    Indeed. Overlooked the obvious - must use new for
    dynamic allocations of C++ classes.

    - Wayne
    Monday, November 1, 2010 2:05 PM