2

I have 3 text fields

  • startnumber
  • Endnumber
  • Limit number

startnumber indicates --->The starting point of random number

End number --> ending point of random number.

Limit number-->how many number we have to display

For example: startnumber is 10 and endnumber is 100 ,if limit number is 5 then I have to display 5 random numbers (without duplicates)

Problem facing:

  • How to display random numbers based on limits without duplicates

Note:
For example if Limit number is 5 in between 10to 100 For example I got below result:{22,44,67,11,11}.
Here 11 is duplicate,using set I can avoid that value but result will become 4.(itsnot accepted).
Instead of 11,I need to display other number then result will becomes 5.(its accepted), its like [Limit number=displayed numbers]


<apex:page controller="randomUtilities">
<apex:form >
    <apex:pageBlock >
        start number: <apex:inputText value="{!startNumber}"/> 
        End Number: <apex:inputText value="{!endNumber}"/> 
        Limit number:<apex:inputText value="{!limitNumber}"/> 
        <apex:pageBlockButtons location="bottom">
            <apex:commandButton action="{!showresult}" value="Here you go...." reRender="as2"/>
        </apex:pageBlockButtons>

    </apex:pageBlock>
    <apex:outputPanel id="as2">
    <apex:pageblock >   
        <apex:pageBlockTable value="{!aclist1}" var="a">
                         <apex:column value="{!a}"/>

        </apex:pageBlockTable> 
    </apex:pageblock>  
    </apex:outputPanel>

</apex:form>


public class randomUtilities {
public integer startNumber{get;set;}
public integer endNumber{get;set;}
public integer limitNumber{get;set;}
public list<integer> aclist{get;set;}
private static integer getRandomNumberInRange(integer min, integer max) {
    return (integer)(Math.random() * ((max - min) + 1)) + min;
}
 public PageReference  showresult(){
    aclist=new List<integer>();
    for (integer i = 0; i <limitNumber; i++) {

        integer a=getRandomNumberInRange(startNumber, endNumber);
        aclist.add(a);
        system.debug('calling'+aclist);
    }
    return null; }}   
3
  • I would anyway use a set and than do a while loop that checks the size of your set. if its < than limit number than add another random number
    – utm
    Jun 29 '18 at 11:15
  • @Annappa instead of using list you can use set, so that you can get unique numbers. Use Set and try the same Jun 29 '18 at 11:45
  • I mentioned that Point Atul.this one is very importnat for me: Limit number=displayed numbers] Jun 29 '18 at 11:52
4

Try in this way.

Integer i = 1;
Integer j= 100; 
set<Integer> setInteger = new set<Integer>();

while (setInteger.size() < 5) 
{
    Integer intRandom = (integer)(Math.random()*(j-i+1)+i);
    setInteger.add(intRandom);
    system.debug('=========inside====');
}
system.debug(setInteger);

enter image description here

1
  • 1
    The if(...contains...) clause is not necessary. A set will not add the same value twice. That's the point of a set.
    – sfdcfox
    Jun 30 '18 at 2:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.