|
1: using System.Collections;
2: public class RandomCollection
3: {
4: private Random rnd = new Random();
5: public int[] RandomSample(int count, int low, int high)
6: {
7: ArrayList rnds = new ArrayList();
8: for(int i = 0; i < count; i++)
9: {
10: int t = rnd.Next(low, high - i);
11: foreach(int r in rnds)
12: {
13: if(t >= r) t++;
14: }
15: rnds.Add(t);
16: }
17: rnds.Sort();
18: return (int[])rnds.ToArray(typeof(int));
19: }
20: }
The card deck is represented as the numbers 1 through 52.
- First you pick a card from the 52 cards available. In line 11, i is 0, so the statement executed does something like int t = rnd.Next(1, 52); . Line 13 isn't executed, since the rnds ArrayList is still is empty.
t represents the first card.
- Second you pick a card from the 51 cards left. Since i is 1, line 11 will dosomething like int t = rnd.Next(1, 51); .
If t is equal to or higher than the first card's number; Add 1. (So that t is in the range of 1 through 52, except for the first card's number.)
- Third you pick a card from the 50 cards left...
If t is equal to or higher than the first card's number; Add 1. (So that t is in the range of 1 through 51, except for the first cards number.)
If t is equal to or higher than the second card's number; Add another 1. (So that t is in the range of 1 through 52, except for the first to cards' number.)
- Fourth you pick a card from the 49 ones left in the deck...
If t is equal to or higher than the first card's number; Add 1. (So that t is in the range of 1 through 50, except for the first cards number.)
If t is equal to or higher than the second card's number; Add another 1. (So that t is in the range of 1 through 51, except for the first to cards' number.)
If t is equal to or higher than the second cards number; Add yet another 1. (So that t is in the range of 1 through 52, except for the first three cards' number.)
There you have it...
NB: The rnds.Sort(); statement in line 17 is NOT optional as previously stated...
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
|
|
|
|
|
thanks a lot!
what is the purpose of rnds.Sort()?
why is it not optional?
|
|
|
|
|
The algorithm will in some cases fail, if the numbers aren't sorted lowest to highest.
Example: If you allready picked 5 and 4 (in that order) and now you're picking 4 again. First check finds that 4 (latest pick) is less than 5 (first pick), so latest pick isn't increased.
The second check will compare 4 (from the latest pick) with 4 (from the second pick) and decide it should increase the latest pick. The latest pick is now 5, and card number 5 can't be picked twice.
When the numbers are sorted, the last pick will increase to 5 on the first comparison and to 6 on the second. Now no cards are beeing picked twice.
Have a look at my latest article about Object Prevalence with Bamboo Prevalence.
|
|
|
|
|
Have you tried using Rand.NextDouble() and doing the multiplication yourself?? That always got new numbers for me. For numbers 1-52 (cards im assuming) do (int)(Rand.NextDouble*52)+1. It seems to give me all different numbers.
|
|
|
|
|
is there a chance it COULD return two or more of the same?
yes, its a blackjack game, and i want to make sure that on the small chance it could happen it would...
thanks.
|
|
|
|
|
Yeah random is random. If it gets 2 or more numbers that end up multiplying out to be together then they will be the same.
|
|
|
|
|
Hello,
Can I make use of an object created in C++ in a program written in C#? If so can someone direct me to any online literature or tips on the subject?
Thanks in advance
|
|
|
|
|
Not without a managed wrapper around it. Your best bet to creating one would be Managed C++. Your C++ code can create the object and manipulate its unmanaged properties and methods and whatnot while exposing a .NET Framework managed interface that your C# code can use.
RageInTheMachine9532
"...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
|
|
|
|
|
Dave's right - a managed wrapper using a mixed-mode Managed C++ assembly would be the easiest. When compiled, the assembly can be used by any other managed language.
You can, however (if you understand P/Invoke and marshaling well), use DllImportAttribute with the CallingConvention.ThisCall and some added class factory functions exported from your native DLL (to create and destroy the class). Read about CallingConvention.ThisCall in the .NET Framework SDK for more information.
Again, though, I'm not recommending this over Dave's reply. I'd definitely go that route; I'm only providing an alternative for completeness.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Heath Stewart wrote:
You can, however (if you understand P/Invoke and marshaling well), use DllImportAttribute with the CallingConvention.ThisCall and some added class factory functions exported from your native DLL (to create and destroy the class).
Heath, how about an article?
top secret xacc-ide 0.0.1
|
|
|
|
|
Sure, when my next mythical time slot opens up.
I actually have discussed this several times here in this forum and have provided a pretty in-depth example. That will have to suffice for now.
It is entirely possible, though, and I have used it in our class libraries before.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Do you have a link to the example perhaps? Or know where I might find any others? I'm interested as well...
Thanks!
|
|
|
|
|
Type "CallingConvention" in the index of your .NET Framework SDK documentation and/or click "Search comments" directly above the message board and search for "CallingConvention" or something. I have over 6,500 posts and only the last 200 are visible using the profile pages. There's no way I could track all this. That's what the search is for.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Sorry, I misunderstood, Was looking for an example of a Managed DLL wrapper, not doing the calling convention.
Search doesn't seem to be working or else nobody has mentioned dll or callingconvention in the past two years
Anyway to reinstate the question does anyone have any examples of a managed C++ dll wrapper? Just would like to see how it's being done.
Thanks!
|
|
|
|
|
|
"Wrapper" is such an ambiguous term, you really need to be specific. Any function or method you call uses a calling convention. When you use ThisCall when marshaling from managed to unmanaged code, the address of the object is pushed onto the execution stack before the first parameter (which becomes the second parameter, and so on).
You can wrap native APIs, too. I've written several MC++ assemblies when I've needed to wrap a whole bunch of APIs (otherwise I just P/Invoke them in C#).
All you do is write Managed C++ and call native APIs. If you want to know more about what makes the difference between managed and unmanaged C++, read Managed Extensions for C++ Programming[^] in the Visual Studio .NET documentation.
You can probably find examples here on CodeProject using the other search at the top of the page below Bob, the CodeProject alien.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
I am pretty new to C# and im trying to access Microsoft Windows volume control. I have tried using interop (via spy++). The problem is that say if i want to mute line in, i can find the WM_COMMAND parameters and put them into my code, and it will do nothing. But i know this code works because when i put in the close window parameters it works fine. So it must be the parameters for the mute button that im not interpereting properly? If anyone knows the parameters to mute and change the volumes for different line channels, or could help in any other way it would be greatly appreciated, cheers
Oldmate
|
|
|
|
|
There's a much better way to change the volume. What you're currently trying to do requires that the Volume window itself be shown just so you can change the slider control. This is incredibly inefficient and error-prone.
Instead, P/Invoke the mciSendString API, part of the MCI APIs which you can read more about at http://msdn.microsoft.com/library/en-us/multimed/htm/_win32_mcisendstring.asp[^]. This would allow you to set the volume without requiring any UI. If you want a UI, use a simple slider control and set the volume according to the current value of the slider whenever a user changes it.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Thanks mate for that, i have found i need to use the winmm.dll for starters, and also i need to use the mixer control. Im pretty sure that i need the mixerMessage to change the volume settings, however as i am pretty new to P/Invoke and these API calls i really dont know where to go from here. I have tried looking up on the net examples, however so far that has prooved unseccussful, if you had any reference to examples using the mixer, or could explain how to use it it would be great, thanks again
|
|
|
|
|
If you don't understand or know something about the .NET Framework, the first place to look and learn is in the .NET Framework SDK[^]. There are several topics you should read, including Consuming Unmanaged DLL Functions[^], Marshaling Data with Platform Invoke[^], and about the DllImportAttribute[^] class.
A good site to check out for P/Invoke signatures (though not always written correctly for 32- and 64-bit platform support, since the use IntPtr for an unmanaged int commonly, which is actually processor-dependent) is pinvoke.net[^]. You won't find the functions for winmm.dll, however.
Below are a couple of examples:
[DllImport("winmm.dll", CharSet=CharSet.Auto)]
[return: MarshalAs(UnmanagedType.U4)]
private static extern int mciSendString(string command, string returnString,
[MarshalAs(UnmanagedType.SysUInt)]IntPtr returnSize, IntPtr callback);
[DllImport("winmm.dll")]
[return: MarshalAs(UnmanagedType.U4)]
private static extern int mixerMessage(IntPtr driverId,
[MarshalAs(UnmanagedType.SysUInt)]IntPtr msg,
[MarshalAs(UnmanagedType.SysUInt)]IntPtr param1,
[MarshalAs(UnmanagedType.SysUInt)]IntPtr param2); There are shortcuts and some attributes are not required, but these signatures will work on both 32- and 64-bit platforms so no assumptions are made.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|
Hi people,
I would like to gather your opinion on the following subject. I am working on a generic O/R mapping tool. Currently, it supports the following database-to-class situations:
1) 1 class per database table
2) 1 class per view which maps into one database table, or maps into several database tables, however, the fields which are required for update, still stay in one table.
I'm thinking of another approach: to have a few database tables for all types of objects in a business domain, something like this:
ClassesTable
Class Type
Entities.Order
Entities.Customer
ClassFieldsTable
FieldID - Class Type - FieldName
1 - Entities.Order - orderID
2 - Entities.Order - orderItem
3 - Entities.Customer - custID
4 - Entities.Customer - custName
FieldValuesTable
ID - FieldID - Value
1 - 1 - ord1
2 - 2 - Sock
3 - 3 - ALADDIN1
4 - 4 - Aladdin
5 - 1 - ord2
6 - 2 - handkerchief
etc.
The tables are related in the following way:
ClassTypes 1 ------ (ClassType) ------- * ClassFieldsTable
FieldValuesTable 1 ------(FieldID) ----- 1 ClassFieldTable
The O/RM layer dynamically constructs SQL commands to store and load business objects
to/from the database, and to perform different types of queries on objects in the
database with the help of object-oriented query builder.
The reasons behind this are:
1) Predefined database structure that will not change with the development of the
business application
2) Possible centralized storage for enterprise-level data to support long-running
business transactions and a common lock mechanizm
3) Still possible to create reports with 3d party tools by using views into the "basic" tables
Can you point me to any disadvantages in this approach?
Regards,
Serge (Logic Software, Easy Projects .NET site)
|
|
|
|
|
1) Performance. For queries, because you always need multiple joins, and for inserts and updates, because you have a bottleneck in the FieldValues table.
2) Data types - you have not mentioned how you will solve the data type problem...you need either 1 table for each data type, or each data type needs to be represented in the FieldValues table, or something inbetween.
Its been done before - I know of one commercial product that uses this technique. I think the project manager did his thesis on it, and concluded that it was a viable way to go.
I'm not so sure.
my blog
|
|
|
|
|
Hi, Steven!
Thanks for your reply.
Steven Campbell wrote:
1) Performance. For queries, because you always need multiple joins, and for inserts and updates, because you have a bottleneck in the FieldValues table.
Can you eloborate a bit on this?
2) Data types - you have not mentioned how you will solve the data type problem...you need either 1 table for each data type, or each data type needs to be represented in the FieldValues table, or something inbetween.
One thing that comes to my mind is to use SQL Server variant data type.
Regards,
Serge (Logic Software, Easy Projects .NET site)
|
|
|
|
|
hai there,
i have few doubts on picture related queries & operations using C#
1. is it possible to read a picture format directly from a scanner using C# ?
2. is it possible to get the type of picture that if i read directly from a hard drive ?
3. i have a .doc or .xls file and i included few pictures using oledb techinics. I need to filter that perticular picture file or files from that .doc or .xls file using my c# code block. Is it possible ?
4. how can i compare picture files.if it is possible give me the technology behind this. Which technology will support this. is it possible with C# ?
Please feel free to contact.
Sreejith S S Nair - Bangalore
|
|
|
|
|
sreejith ss nair wrote:
1. is it possible to read a picture format directly from a scanner using C# ?
Yes, with a TWAIN driver wrapped in managed code. I'm not sure of any third-party libraries that may exist (they probably do), but writing this requires a lot of knowledge of device drivers and TWAIN. You're best-off just having the scanner save the image and read it into managed code, or find a third-party library that does enscapsulate TWAIN drivers for your scanner. You may also be able to leverage WIA (Windows Image Acquisition).
sreejith ss nair wrote:
2. is it possible to get the type of picture that if i read directly from a hard drive ?
What? If you read it from a stream without knowing the ImageFormat ? Use the Image.RawFormat property (inheritted by the Bitmap class).
sreejith ss nair wrote:
3. i have a .doc or .xls file and i included few pictures using oledb techinics. I need to filter that perticular picture file or files from that .doc or .xls file using my c# code block. Is it possible ?
Yes. You'll need to enumerate the objects in the compound document and determine what type they are. There are documented APIs in the Office automation documentation that can be installed with Office that detail this.
sreejith ss nair wrote:
4. how can i compare picture files.if it is possible give me the technology behind this. Which technology will support this. is it possible with C# ?
You need to perform a pixel-by-pixel comparison. Use LockBits on two Bitmap s and read through the BitmapData comparing the pixels of each image.
Take a look at a reply I wrote a day or two again in this forum about how to compare two MemoryStream s. The concept is the same, though the manner is a little different. You just loop through the pixels (may be multiple bytes) and compare them. Using unmanaged code is a little faster. See Christian Graus's articles on imaging in .NET for examples.
Microsoft MVP, Visual C#
My Articles
|
|
|
|
|