Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
|
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 31st, 2008, 12:58 AM
Authorized User
 
Join Date: Jan 2008
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Random Number Generation

Hi All
    I have following problem with random number generation. I have 10 images, which i have to display in random order each time i refresh my form, but the problem is that .net random function generates the same sequence each time.

Here is my Code

public static DataTable GetRandomJobLogos(DataTable dataTable){
int totalRows = jobSourceDataTable.Rows.Count;
DataTable tempDataTable = dataTable.Clone();
ArrayList numberList = new ArrayList();
Random rand = new Random(0);
for (int i = 0; i < totalRows; i++){
   int randomRowNum = rand.Next(totalRows);
   if (!numberList.Contains(randomRowNum)){
       numberList.Add(randomRowNum);
       tempDataTable.ImportRow(dataTable.Rows[randomRowNum]);
   }
   else{
       i--;
   }
}
}
entries in tempDataTable are always in same order.
am i doing some wrong, or there can be any better way to do this?

 
Old March 31st, 2008, 03:33 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

There are two constructors for the Random class. The one you are using takes an integer as the seed for the random sequence. There is another on which takes no parameters and uses the current time as the seed value.

Random rand = new Random();

/- Sam Judson : Wrox Technical Editor -/
 
Old March 31st, 2008, 10:50 PM
Authorized User
 
Join Date: Jan 2008
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That works. Thanx for reply sam.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Generation of alphanumeric random characters sajid_pk ASP.NET 1.0 and 1.1 Professional 1 May 10th, 2005 08:37 AM
random file generation Ashleek007 Beginning PHP 1 February 24th, 2005 03:20 AM
random number rajuru Beginning PHP 7 December 7th, 2004 10:52 AM
Random Generation of Username and Password Prabhakar_dt Classic ASP Basics 2 August 31st, 2004 06:45 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.