function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
shephalishephali 

how to ganerate random 6 digit number

hello friends,
   I m using following code to ganarate 6 digit random number.but it creates 4-6 digit random number(eg. like 4567,98765,341235,...).But i need 6 digit random numbers only.how to solve this.
Integer index=0;  
    public Integer getindex()
   {
      double y = Math.random() ;//something between 0 and 999999  
      y = 999999 * y;
      index = math.round(y); 
    return index;
    }
Best Answer chosen by shephali
William TranWilliam Tran
Try this:
 
​public Integer getindex(){
return Math.round((Math.random() * (900000) + 100000));
}
As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks

 

All Answers

William TranWilliam Tran
Try this:
 
​public Integer getindex(){
return Math.round((Math.random() * (900000) + 100000));
}
As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks

 
This was selected as the best answer
shephalishephali
Thanks @William Tran!!!
It worked.
Fajer NaqeebFajer Naqeeb
i want to know the where that random number is getting reflected