Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old March 31st, 2008, 12:58 AM
Authorized User
 
Join Date: Jan 2008
Location: Islamabad, Punjab, Pakistan.
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?

  #2 (permalink)  
Old March 31st, 2008, 03:33 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
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 -/
  #3 (permalink)  
Old March 31st, 2008, 10:50 PM
Authorized User
 
Join Date: Jan 2008
Location: Islamabad, Punjab, Pakistan.
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That works. Thanx for reply sam.

 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 05:40 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.