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
viswadaviswada 

Randomly display records error

HI All,

 

     I have  to display 20 questions randomly to vf page ,  i have  67 records in my object,.   some times it's dispaly 20 questons , but some times sit diaplaying  less than 20 records,  If rand value  reaches to 67  it not diapaying lessthan  20 records  , wha  is ther any alternative to to display random records. my controller is

  

public class questions2 {

public string answer1{set;get;}
public List<selectOption> selItems {get;set;}
public integer intCount{get;set;}
list<wrapperQuestions> objwraplst = new list<wrapperQuestions>();
list<Questiuons__c> ques=new list<Questiuons__c>();
public questions2()
{
integer serial=0;
Integer count = [SELECT COUNT() FROM Questiuons__c ];
system.debug('$$$$$$$$$$$$$$$'+count);
Integer rand = Math.floor(Math.random() * count).intValue();
system.debug('@@@@@@@@@@@@'+rand);
for(Questiuons__c a :[SELECT id,Question__c,Answers__c,Correctanswer__c FROM Questiuons__c LIMIT 20 OFFSET :rand])
{
System.debug('aaa'+a);
String s = a.Answers__c;
list<string> st =s.split(',');
wrapperQuestions objWrap = new wrapperQuestions();
objWrap.strQuestion = a.Question__c;
objWrap.correctAnswer=a.Correctanswer__c;
//serial=serial
serial=serial+1;
objWrap.intSerial=serial;
selItems = new List<selectOption>();
for(String str:st)
{
selItems.add(new SelectOption(str,str));

objWrap.itemsWrap=selItems;
//objWrap.itemsWrap.add(new SelectOption(str,str));
}

objwraplst.add(objWrap);

}
}

public void saveAnswer()
{
intCount=0;
for(wrapperQuestions objwrap:objwraplst)
{
system.debug('::::::::::::'+objwrap.answer12);
if(objwrap.answer12==objwrap.correctAnswer)
{

intCount++;
}
}

}
public list<wrapperQuestions> getitems1()
{
return objwraplst;
}

public class wrapperQuestions
{
public string strQuestion{get;set;}
public integer intSerial{get;set;}
public List<selectOption> itemsWrap {get;set;}
public string answer12{set;get;}
public string correctAnswer{set;get;}

}
}

     anyone can help me 

Best Answer chosen by Admin (Salesforce Developers) 
viswadaviswada

HI  Mpann,

 

               Thanks alot,  Now its  woking 

  

All Answers

mpannmpann

I don't think you can use the Select statement to produce a truly randomized list and the OFFSET simply skips over x rows.

 

I would try to get all questions, put them in a list and randomize that list. Then your visual force page only displays the 20.

viswadaviswada

 Thanks for response ,

 

 

 My senario is I have  20 records in  my Qustions objects, i need to diplay  all reocrds in random manner, if   i refresh the page  i  the order of records should be changed , how can we do it  please help me

mpannmpann

I wrote a little utiltiy class, you can use to randomize a list of sObjects:

 

public class ObjectRandomizer {

	public static list<sObject> randomizeList (list<sObject> records){
	
	list <sObject> retList = new list <sObject>();
		
		while (records.size()>0){
			retList.add(records.remove(getRandomInt(records.size())));
		}
		
		return retList;
	}

	public static Integer getRandomInt (Integer num){
		Double d = math.random() * num;
		return d.intValue();
	}

}

 So your controller needs to retrieve all questions and then call the ObjectRandomzier.randomizeList. When your VF page asks for the list of questions, just return the first 20 of that list.

viswadaviswada

HI  Mpann,

 

               Thanks alot,  Now its  woking 

  

This was selected as the best answer