.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

Random Function

Posted By: Aamrapali Tamgadge     Posted Date: January 07, 2011    Points:2   Category :ASP.Net
hello sir,
Me having one random function
private int RandomNumber(int min, int max)
{
Random random = new Random();
return random.Next(min, max);
}
and i hv call it like this

int returnValue = RandomNumber(2, 4);
Convert.ToString(Convert.ToInt32(Session["counter"].ToString()) + 1);
getQuestion(returnValue);


but this is not working Properly
i want the values from 2 to 4
but in above function its getting repeated first value 2


Responses
Author: Sadhuryan             
Posted Date: January 07, 2011     Points: 5   

Hi Aamrapali,


Exact problem is with your declaration of 'Random' object within a function.

Try this way:

Public static class Program
{
//Declare the Random object in Class level (i.e. inside your class, not inside a function)
private static Random rand = new Random();

public void static Main()
{
//Call the object inside your function
Console.WriteLine(string.format("A random Number within 1-50: {0}", rand.Next(1,50));
Console.ReadKey();
}
}



Author: Aamrapali Tamgadge             
Posted Date: January 07, 2011     Points: 5   

Hi,Sadhuryan,
I have impliment your code in my application but me having one online exam website app and i want to display questions randomly but i have problems with this is dat same que is getting repeated.what shoud i do for this not to repeat the same que.

Software Programmer At OTIS ISRC
Author: Aamrapali Tamgadge             
Posted Date: January 07, 2011     Points: 5   

even i declare Random rand = new Random(); in class level same problem arise please suggest me (argent
)

Software Programmer At OTIS ISRC
Author: Sadhuryan             
Posted Date: January 07, 2011     Points: 5   


Hi Aamrapali,

Logic must be,

1) Create a temporary question list.
2) Call your randomizing function to pick a question for display.
3) After display, Selected question must be removed from the lot.
4) On clicking 'Next' button, Now again call the function to pick a question from the lot.
Here, the questions will never get repeated.

Is this clear now..? good luck .

Revert back to me, in case of queries.

Author: Aamrapali Tamgadge             
Posted Date: January 07, 2011     Points: 5   

Actualy fist time wen the page get loaded i have display the first que but den now from que no 2 to next i want to pick up from database table den disply

Software Programmer At OTIS ISRC
Author: Aamrapali Tamgadge             
Posted Date: January 07, 2011     Points: 5   

But still it getting repeated


Software Programmer At OTIS ISRC
Author: Manas Patnaik             
Posted Date: January 07, 2011     Points: 5   

Hi , I guess the approach to the problem mentioned is somehow not correct.
The Random class will fetch you the Random number based on the Seed and i see the seed you have mentioned is always same.

The best way to do is to fetch the random quations from database while load instead of hitting database each time.

Use following query to fetch unique records from database based on GUID.

SELECT TOP 5 * FROM Table ORDER BY newid()




Manas Patnaik
http://manaspatnaik.com/blog


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend