Omar Al Hammal on Tue, 25 Sep 2012 13:05:03 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Question about random.


  Dear all,

 I am writing because I am fairly new to gp/pari and think to have found a mistake in some codes I have to use. I would like to have some opinions to know whether or not it indeed is a mistake. 

 Here is part of the code I use. 

 specieslabelanc = vector(J);
 \\ with a<J
 z = ceil(random/2^31 * (a - 1));
 specieslabelind[n] = specieslabelanc[z];

  It works almost always but sometimes give a mistake saying 0 is not in the range [1:J]. I think this is because "random" can be 0 (not often but it definitely happens) and therefore ceil can return 0.


The corrected version I now work with is :

longestint=2^31;
longestintm=2^31-1;

specieslabelanc = vector(J);
 \\ with a<J
z = ceil((random(longestintm)+1)/longestint * (a - 1));
 specieslabelind[n] = specieslabelanc[z];
 
 Thanks,

Omar