Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
  • 1.  random number generation

    Posted 02/02/09 05:25 AM

    Originally posted by: SystemAdmin


    [eissa said:]

    Hello Everyone,

    I am try to generate some random data in OPL using rand() and srand() functions, but everytime I run the code it gives me exactly the same set of numbers and actually some patterened numbers! Does anyone know what the problem would be?

    I went through all the examples in help, but unfortunately the is no example using random generated data!
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 2.  Re: random number generation

    Posted 02/02/09 01:58 PM

    Originally posted by: SystemAdmin


    [jfk said:]

    Hi,
    well, if you read the doc you will see that it is not random, but pseudorandom stuff: ILOG OPL Development Studio 6.1.1 > Language > Language Quick Reference > OPL functions > rand
    It means that there is an algorithm which generate the random numbers and if the seed - this is the number to compute the first random number - of the algorithm is always the same, then the random numbers will be always the same. This is the nature of the thing. You could randomize truly if you chose some random event for the seed, like the 100th sec of the actual time or something like that.

    I hope it helps

    cheers 
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 3.  Re: random number generation

    Posted 02/02/09 08:24 PM

    Originally posted by: SystemAdmin


    [eissa said:]

    Thank you katai for your reply.

    But, for example if I use the following simple code:
    -------------
    int a = 10;
    int b = srand(a);
    int c = rand();
    execute
    {
    writeln(c);
    }
    --------------------
    no matter what I choose for a the output is the same! Can you tell me what I am missing here?

    Eissa
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 4.  Re: random number generation

    Posted 03/04/09 02:37 PM

    Originally posted by: SystemAdmin


    [afleischer said:]

    maybe you could try

    float c;


    execute
    {
    c=Math.random();
    }

    int c2=ftoi(round(c*100));



    execute
    {
      writeln(c2);
    }

    or simply

    float c;


    execute
    {
    c=Math.random();
    }

    execute
    {
      writeln(c);
    }

    if you do not want c to be an int but a float between 0 and 1

    Alex
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 5.  Re: random number generation

    Posted 11/02/11 01:39 PM

    Originally posted by: JackJackson


    The statements: int a=10; int b = srand(a);
    sets the random number seed to the same value each time (10).
    So you get the same stream of psuedo-random numbers each time.
    Change 'a' to a different integer if you want a new stream.
    You will then get different input. (Almost every time.)
    You should get different output.
    #DecisionOptimization
    #OPLusingCPOptimizer


  • 6.  Re: random number generation

    Posted 04/12/12 06:38 PM

    Originally posted by: Karthick.Kanagal


    a way of randomizing to get a value from 0 - 1, if you want something different just remove the the "/100" from float r

    int mySeed;
    execute{
    var now = new Date();
    mySeed = Opl.srand(Math.round(now.getTime()/1000));
    }

    int b = srand(mySeed);

    float r = rand(100)/100;

    execute {
    writeln (b);

    writeln (r);}
    #DecisionOptimization
    #OPLusingCPOptimizer