Jump to content

Generating Non Repeatable Random No...


LOSTBOY

Recommended Posts

Hi Experts

   Am an PHP Beginner

 here i give snap of my code ..... there how can i generate non repeating random nos...............this code didnt  work properly what can i do for that

  do{  

 $qno=rand(1, $_SESSION['$max']);  

 if (!in_array($qno,$tempArray)){

   $_SESSION[$tempArray[$r]] = $qno;

$i=0;

$query="SELECT * FROM question WHERE qid='$qno' " ;

   $result=mysql_query($query);

   $id=mysql_result($result,$i,"qid");

   $question=mysql_result($result,$i,"question");

   mysql_result($result,$i,"question");

   $query1="SELECT * from answer where qid=$id";

   $result1=mysql_query($query1);

$r++ ;

 }

}while($r <= $n)

 

 

 

thanks in advance

Link to comment
Share on other sites

thanks for ur valuable suggestion ..sorry ......am an newbie  to this forum....

  $r=0
  do{   
  $qno=rand(1, $_SESSION['$max']);   
  if (!in_array($qno,$tempArray)){ 
    $tempArray[$r] = $qno; 
    $query="SELECT * FROM question WHERE qid='$qno' " ;
    $result=mysql_query($query);
    $id=mysql_result($result,$i,"qid");
    $question=mysql_result($result,$i,"question");
    mysql_result($result,$i,"question");
    $query1="SELECT * from answer where qid=$id";
    $result1=mysql_query($query1);
    $r++ ;
  } 
}while($r <= $n)

Link to comment
Share on other sites

>sorry ......am an new one to this forum....

 

As we all were at one time.. no worries = )

 

<?php
  do{   
  $qno=rand(1, $_SESSION['$max']);   
  if (!in_array($qno,$tempArray)){ 
    $tempArray[$r] = $qno; 
     v$i=0;

 

v$i is not a valid PHP variable. Was that a typo?

Maybe you meant $i? or $vi?

 

Fix that and see where you are. If it doesn't work, tell us what error you recieve or what result you recieve that isn't what you are expecting.

 

PhREEEk

 

Link to comment
Share on other sites

And what will be your $_SESSION['$max'] maximum value, if it is low then there is max chance of repeating. Try using mt_rand instead. Not too sure but give a try  ;D

$qno=mt_rand(1, $_SESSION['$max']); 

 

It looks like you are trying to fetch a random question from DB, if it is right then you can try ORDER BY RAND() in your select.

There is a sticky out there in MySQL board.

Hope it helps

Link to comment
Share on other sites

Anyway try this spaghetti code of mine

 

  do
  {   
    $qno=rand(1, $_SESSION['max']);   
    if (!in_array($qno,$tempArray)) 
      $tempArray[] = $qno; 
  }while(count($tempArray)<$_SESSION['max']);
//  This will ensure that all elements of tempArray is unique and has count equal to max

  for($i=0;$i<count($tempArray;$i++)
  {
      $query="SELECT q.qid,q.question,a.answer FROM question q JOIN answer a ON a.qid=a.qid WHERE qid=$tempArray[$i] " ;
      $result=mysql_query($query);
      $questionandanswer[]=mysql_fetch_row($result,MYSQL_ASSOC))
  }
// This will get all records of from your db with qid equal to the qids in your tempArray

Link to comment
Share on other sites

In that case let's revise the last code I posted and since you are using sessions. Try this though I haven't tested this (working on imagination)

 

  $tempArray=$_SESSION['questionsandanswer'];  // Make sure you get the value for the array
  $flag=0;
do
  {   
    $qno=rand(1, $_SESSION['max']);   
    if (!in_array($qno,$tempArray)) 
    {
      $tempArray[] = $qno; 
      $flag = 1;  // Used this to indicate that a record is added
    }
  }while(count($tempArray)<$_SESSION['max'] && $flag=0);
//  This will ensure that all elements of tempArray is unique and has count equal to max
//  This will only add one record to your tempArray every call to this page

  $id=$tempArray[count($tempArray)-1];
  $query="SELECT q.qid,q.question,a.answer FROM question q JOIN answer a ON a.qid=a.qid WHERE qid=$tempArray[$id] " ;
  $result=mysql_query($query);
  $questionandanswer=mysql_fetch_row($result,MYSQL_ASSOC))
// This will get the last record of that tempArray
  $_SESSION['questionandanswer']=$tempArray;
// Make sure you pass the updated tempArray

 

Sorry for the spaghetti code guys...

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.