|
Using OrderBy with a random number generator is a good trick.
|
|
|
|
|
You might want to check the Computerphile channel on youtube who do a number of very good presentations on sorting arrays(I take it when you say shuffle you mean sort).
The short answer is that there is no fastest method that fits all scenarios - depending on the number of elements and distribution different methods will be faster or slower. The main factor influencing which sort algorithm to use is the size of the array.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
modified 11-Mar-14 5:02am.
|
|
|
|
|
The use of the word "random" indicates the question is, indeed, about shuffling, not sorting.
“The best hope is that one of these days the Ground will get disgusted enough just to walk away ~ leaving people with nothing more to stand ON than what they have so bloody well stood FOR up to now.” Kenneth Patchen, Poet
|
|
|
|
|
I don't want to sort. I want to shuffle (mixed up, disorganize).
|
|
|
|
|
When you express a problem, it's helpful to think in terms of what you are actually trying to solve. So, when you say the shortest way, this could mean many different things. Do you mean that, given an arbitrarily large array, you want the fastest shuffle? Should it be the fewest number of lines? Any of a half dozen other possible meanings of shortest?
You will get a lot of guesses and assumptions about what the problem is that you are trying to solve - especially when you show a code snippet that doesn't have any real relation to the problem other than it's got an array in it.
|
|
|
|
|
This generic function provides a quick way of randomly shuffling (reordering) the items in-place in an existing array:
public static void ShuffleArray<T>(T[] array)
{
Random rnd = new Random();
int[] order = new int[array.Length];
for (int i = 0; i < array.Length; i++)
{
order[i] = rnd.Next();
}
Array.Sort(order, array);
}
It can we rewritten as
Random rnd = new Random();
Array.Sort(array.Select(r => rnd.Next()).ToArray(), array);
giving just slightly worse performance.
According to my tests the use of Array.Sort is about three times faster than the OrderBy solution for a million integer array.
|
|
|
|
|
Hi again,
using the Fisher-Yates shuffling algorithm [^] seems to be superior in performance:
public static void Shuffle<t>(T[] array)
{
Random rnd = new Random();
for (int i = array.Length - 1; i >= 0; i--)
{
int index = rnd.Next(i);
T temp = array[index];
array[index] = array[i];
array[i] = temp;
}
}
|
|
|
|
|
Hi,
I am working on Winform application.we have One publisher and 2 subscribers.we are using merge replication. it is taking at least 30 min to sync all subscribers and publisher. I want to have all my users live data all the time.I read on MSDN that we can force Sync by using RMO programming.What are the disadvantages of doing this?
are there any performance issues?I am new to this RMO Programming.Can anybody suggest any real time example or tutorial on this topic?
Thanks
|
|
|
|
|
Member 10640959 wrote: Can anybody suggest any real time example or tutorial on this topic? There's always MSDN[^] of course.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
programmatically print button for rdlc report using C# in asp.net .. plz help me.
|
|
|
|
|
Do you have a specific question?
/ravi
|
|
|
|
|
|
create objects in C# and get the data from sql in the objects in c# windows application.
|
|
|
|
|
OK, and what is your question?
|
|
|
|
|
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
The trouble is that there are so many ways to retrieve info from SQL, and since everything in C# is object based or derived, we can't just go "do this" and it'll all sort itself out.
So sit back, think about what you are trying to do and give us better information!
Remember, the better the info you give us, the better or response can be.
So help us to help you!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Have a read of these Codeproject articles. They should show you the basics of connecting to a database and how to retrieve data. Also try out the samples and modify them as this is one of the best ways to start learning something in my opinion.
Using ADO.NET for beginners[^]
An Introduction to Entity Framework for Absolute Beginners[^]
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
I have a set of tiff images. I converted those images to PDF using iTextSharp dll. I want to add bookmarks in that PDF file. Does it possible? I tried with the below code:
string bookMarkName = inputCriteria[0].IndexName;
Chapter chapter1 = new Chapter(new Paragraph(bookMarkName), 1);
chapter1.BookmarkTitle = bookMarkName;
chapter1.BookmarkOpen = true;
document.Add(chapter1);
Even though the PDF file is created with bookmarks, it simply added the bookMarkname in every page as bookMark even though that string is not available in that particular page. Is there any other way which I can achieve this? Please help.
|
|
|
|
|
Hi Guyz,
I am developing windows metro apps using vb.net and XAML.
I need to convert canvas element which contains ink strokes on it.
I am using the below code :
<grid grid.row="1">
<grid.rowdefinitions>
<rowdefinition height="1*">
<rowdefinition height="1*">
<grid.columndefinitions>
<columndefinition width="1*">
<columndefinition width="1*">
<stackpanel x:name="spCollectionSignature" margin="120,0,10,10">
<textblock text="Client signature at collection address" style="{StaticResource FieldTextStyle}" padding="0,5,5,5">
<textblock style="{StaticResource LabelTextStyle}" padding="0,5,5,5" textwrapping="WrapWholeWords" maxheight="120" texttrimming="WordEllipsis" text="{Binding PCollectionSignatureMessage, Source={StaticResource oSettingData}}">
<border x:name="borderCollectionSignature" horizontalalignment="Left" verticalalignment="Top" borderthickness="2" borderbrush="{StaticResource AppBorderBrush}">
<canvas x:name="CollectionSignature" height="120" width="350" background="White" margin="0" horizontalalignment="Left">
Vb.Net code :
Public Shared Async Function GetSignatureImageString(oElement As Canvas) As Task(Of String)
Try
Dim o As New RenderTargetBitmap
Await o.RenderAsync(oElement, Convert.ToInt32(oElement.Width), Convert.ToInt32(oElement.Height))
Dim aryData = Await o.GetPixelsAsync()
Dim logicaldpi = DisplayInformation.GetForCurrentView().LogicalDpi
Dim oStorageFile As StorageFile = Await ApplicationData.Current.TemporaryFolder.CreateFileAsync(oElement.Name + ".png", CreationCollisionOption.ReplaceExisting)
Dim oStream As IRandomAccessStream = Await oStorageFile.OpenAsync(FileAccessMode.ReadWrite)
Dim oEncoder As BitmapEncoder = Await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, oStream)
oEncoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, Convert.ToInt16(oElement.Width), Convert.ToInt16(oElement.Height), logicaldpi, logicaldpi, aryData.ToArray())
Await oEncoder.FlushAsync()
Dim oBuffer As IBuffer = Await FileIO.ReadBufferAsync(oStorageFile)
Dim aryData1 As Byte() = oBuffer.ToArray()
oStream.Dispose()
oEncoder = Nothing
oStorageFile = Nothing
aryData = Nothing
o = Nothing
Return Convert.ToBase64String(aryData1)
Catch ex As Exception
End Try
End Function
I am getting 'Value does not false within expected range' on 'o.renderasync' method.
Can you please help?
Thanks,
Vinay
|
|
|
|
|
Rule 1: Learn to read.
See the big bit at the top of the page that says which forum it is: "C#"
That means it's a forum for C# questions, not VB.
This is not a C# question, so post it in the correct place.
Here: http://www.codeproject.com/Forums/1646/Visual-Basic.aspx[^]
Or here: http://www.codeproject.com/Questions/ask.aspx[^]
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Hi OrginalGriff,
Thanks...
I have posted the same question on the VB forum.
|
|
|
|
|
I m creating a datagridviewcombobox at runtime ,in the items i have a item :--New--, when the user selects --New-- item , i want the combobox to be removed or changed to textbox control so that i can accept the input form the user.
Please suggest any help or any way to achieve this.
|
|
|
|
|
|
I need to print a specific page of a PDF that contains many pages in windows application using C#.
Thanks in advance.
|
|
|
|
|
Don not post the same thing in two different places: it duplicates work and annoys people.
You have this in QA, leave it there.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Hi,
I've 4 years experience in C# with Winfroms,wpf,silverlight. I placed my goal as become good technical architect in next two years. If you know the route map to reach, please guide me, where to start.
|
|
|
|