|
I have just completed a course in programming VB.NET with DeVry and I really like it. I want to write a program for a blackjack game. I have 2 arrays set up for the deck of cards, one for the card name (ex. 3-Hearts) and the other for the card value (ex. the 3-Hearts = 3).
I'm starting small and I only want to deal 2 hands (user and dealer). My question is:
Is there a better way to generate 4 random numbers from 1 - 52 so that you do not get the same number twice? Of course, for drawing other cards in the hand I'll need additional random numbers.
This is what I have and it seems to work, but is there another (better, shorter) way?
Randomize() 'Initialize random number generator.
Do
intCard1 = CInt(Int((51 - 0 + 1) * Rnd() + 0)) 'Random generate 1st number.
intCard2 = CInt(Int((51 - 0 + 1) * Rnd() + 0)) 'Random generate 2nd number.
Loop Until intCard1 <> intCard2 'Loop if cards are the same.
Do
intCard3 = CInt(Int((51 - 0 + 1) * Rnd() + 0)) 'Random generate 3rd number.
Loop Until intCard3 <> intCard1 And intCard3 <> intCard2
Do
intCard4 = CInt(Int((51 - 0 + 1) * Rnd() + 0)) 'Random generate 4th number.
Loop Until intCard4 <> intCard1 And intCard4 <> intCard2 And intCard4 <> intCard3
Thanks in advance for any assistance!
Mike
|
|
|
|
|
The trick is simulating what happens in real world.
Suppose you have to extract five (distinct) numbers from fifty ones.
You first initialize an array
For n=1 to 50
num(n)=n
next n
then you extract the five numbers into the extracted array
for k=0 to 4
r = CInt(50 * Rnd() + 1 - k))
extracted(k) = num(r)
swap(num(r), 50 - k )
next k
As you can see, each time the extracted value is swapped with the element at the and of the array and the following choice is done on a narrowed array.
Hope that helps (I wrote the code remembering the algo, i.e. I did not verify it, only to illustrate the algo itself).
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Thanks for the prompt response. I thought I would try a small program using your suggestion just to see if I could generate the random numbers to a msgBox, but I get the dreaded blue squiggly line under "swap" and the message says that the "expression is not a method". Any ideas on this?
Thanks again,
Mike
|
|
|
|
|
If there is no method to swap two variables then you have to manually swap them, i.e.
swap(a,b)
is equal to the following statements:
tmp=a
a=b
b=tmp
where tmp is a temporary variable.
hope that helps
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
This is the error message that i receive when i go to run the program.
Windowsapplication1 has encountered a problem and must shut down.
Error Signature:
EventType: clr20r3 P1:applaunch.exe P2: 2.0.50727.42
P3: 4333ab62 P4:starch P5:1.0.0.0 P6: 45952711 P7:e
P8: e9 P9:system.invalidoperationexception
|
|
|
|
|
There's no way to tell from that information. There's LOTS of possible causes, including making cross-thread calls in .NET 2.0 that are no illegal but worked fine under .NET 1.x. Messing up the references section of your code. Interdependencies of any controls your using, like using the Office 11 PIA's and only having Office 10 installed. Controls that depend on other libraries that don't exist on the target machine. ...
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
anyway to further identify the problem?
|
|
|
|
|
Nope. The data that's in the message is just the data of the message itself.
If you can get a debugger attached to it you're well on your way to finding the cause. The problem is that most of the time, this error happens right at startup, which would lead me to believe that there is a dependancy problem.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
ok so this might be a stupid question, but how do i go about getting a debugger attached to the program
|
|
|
|
|
Create a test machine that replicates the problem, then install VS.NET on it. Or setup a remote debugging session so you can attach your development machine to the process on a remote machine. But, you have to launch the code to get the process running before you can attach the debugger to it. Like I said, if it crashes immediately upon launch, you're not going to get a debugger going and attached.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Hi all,
Can I run vb code and com components in Unix, Lynux and Solaris operating systems. I need complete information
Thank you,
S/W Engineer
Akebono Soft Technologies
aleem_abdul@akebonosoft.com.
|
|
|
|
|
No.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
I am using bound DataGridView with vb.net 2005 Express edition.
First I add the database to the project
Then I select the table which I want on the form and drag it on the Form.
I drop it there and a DataGridView comesup with the toolbar on top.
The problem is that Update/Save doesn't work.
Update button has following code beneath itself
Private Sub MainBindingNavigatorSaveItem_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MainBindingNavigatorSaveItem.Click<br />
Me.Validate()<br />
Me.MainBindingSource.EndEdit()<br />
Me.MainTableAdapter.Update(Me.PipeDataSet.Main)<br />
<br />
End Sub
can someone help, Please
Think, and it can be done
|
|
|
|
|
Hi all,
HiEdit V1.0.1.4 has been released. HiEdit is a multi-tabbed, high speed, large file edit control. It uses no more than ~140K of memory due to its sophisticated memory management. It supports most standard edit/richedit control messages and many custom ones. You can use it with any programming language since it is packaged as a standard dll and a static library. A full source demo application (written in Assembly) is inluded. You can get it at http://www.winasm.net/index.php?ind=downloads&op=entry_view&iden=151. I am currenlty writting a demo application in VB6 which I plan to upload soon.
In addition, you can get HiEditor V1.0.1.5 (a full blown application that uses HiEdit).
Any feedback/suggestions is highly appreciated
Regards,
Antonis Kyprianou
http://www.winasm.net
|
|
|
|
|
What is it about "No advertising or soliciting" that is beyond your comprehension?
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
I apologize if this post is considered as an ad. Please, admins, delete this post. How does one let other programmers know about a free project (open source or not) without being considered as an ad?
http://www.winasm.net
|
|
|
|
|
akyprian wrote: How does one let other programmers know about a free project (open source or not) without being considered as an ad?
Write an article about. But then, please, don't advertise the article.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
I see. Sorry for the inconvenience. I was not aware of how things work here.
Thanks for your kindness to explain,
Antonis
http://www.winasm.net
|
|
|
|
|
Dear friends,
How can I take the backup of SQL server database through coding, and I want to restore it through coding. The database must
be kept as a text file. And shuld be able to restore it.
And how can I import and export the SQL server data in to MS-Access mdb file. Please advise me, or give me some code snippet
if possible. Thank you.
S/W Engineer
Akebono Soft Technologies
aleem_abdul@akebonosoft.com.
|
|
|
|
|
Hai
I have created a mdi form the problem is I am not able to Resize the Form and Align the image(From the BacKground Property)to the center of the MdiForm.But I don't wan't the other forms to be disturbed from displaying from the main menu of the MDI Form
sashirekha
|
|
|
|
|
HI I M NEW HERE, I WANT A LITTLE HELP , I M CREATING A SOUND PROJECT , FOR THAT I WANT UR HELP , PLEASE TELL ME HOW CAN I DIFFERNTIATE BETWEEN HEIGHTS OF DIFFERENT WAVES WHICH I INPUT THROUGH MIC (HOW CAN I CALCULATE THE HEIGHT OF TWO DIFFERENT SOUND WAVES)??????????????? PLEASE HURRY ITS VERY URGENT....
Dark Verdict
|
|
|
|
|
First, I'm going to direct your attention to point number 6 in this[^] post.
Second, if it was that urgent you'd be Googling this 'till your fingers bled. Telling us it "VERY URGENT", statistically, means that you've procrastinated on a hoemwork assignment or class project and now have to write the entire app in 1-2 days. Is this true?
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Dave have you ever considered giving up programming and getting into psychology?
not even far from google, but just here you'll find this interesting program
for audio analysis of wav files, and
read the code, you'll see a place where it returns the amplitude of a signal at a certain
frequency (and then draws it)
http://www.codeproject.com/vb/net/waverender.asp
and another more complex, and use of fast fourier
http://www.codeproject.com/audio/waveInFFT.asp
hope that helps
|
|
|
|
|
ad3z wrote: Dave have you ever considered giving up programming and getting into psychology?
Sometimes, it absolutely amazes me that so many people have no idea, what-so-ever, how to do any kind of research into a topic, let alone have any kind of clue what it takes to do something with a computer.
Way too many of these questions, now-a-days, are comming acrossed like "Isn't there any kind of a library that will do this for me?".
ad3z wrote: not even far from google, but just here you'll find this interesting program
for audio analysis of wav files, and
It won't help me, but it might help the OP if you reply to him instead.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|
|
Dave Kreskowiak wrote: it absolutely amazes me that so many people have no idea, what-so-ever, how to do any kind of research into a topic, let alone have any kind of clue what it takes to do something with a computer.
You're not the only one who is absolutely amazed, count me as well
|
|
|
|