Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I'm trying to get 9 numbers from 0 to 8 using this

 


intButtonNumber = CInt(Int((8* Rnd())))

 

I am then testing using an array if the random number has already come out and if so loop until an unpicked number appears.

 

Trouble is it stays in the loop forever, numbers like 4, 6 and 3 keep repeating?

 

Any ideas please?:(

My website
Posted
The random numbers will never use the complete range provided, you will probably end up writing some fiddily code on, try adding 1 to the number drawn until its one that hasn't been chosen, if it exceeds nine then return to 0 and keep trying
.Net allows software to be written for any version of Windows and not break like Unmanaged applications unless using Unmanaged procedures like APIs. If your program uses large amounts of memory but releases it when something else needs it, then what's the problem?
Posted

I thank you :-)

 


For intX = 0 To 8

   Do While True

               intButtonNumber = CInt(Int((8 + 1) * Rnd()))

               If intButtonNumber < 9 AndAlso intButtonsSet(intButtonNumber) = 0 Then

                   DirectCast(m_htControlsHashTable("Button" & intX + 1), Button).Location = intZones(intButtonNumber)

                   intButtonsSet(intButtonNumber) = -1

                   Exit Do

               End If

     Loop

Next

My website
  • *Experts*
Posted

I wouldn't use the Rnd() function anymore. It's another holdover from VB6 that's been replaced by the FAR better Random object.

 

Dim i As Int32
Dim r As Random = New Random()
For i = 0 To 99
   System.Diagnostics.Debug.WriteLine(i & " " & r.Next(0, 10))
Next

 

There are other overloads for the Next method. There are also other methods (NextDouble and NextBytes).

 

You'll still have to add 1 to the Max just as before. So for a range of numbers 0-9, use .Next(0, 10).

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Nice one...

 

Tell me, is there any easy way to find the new stuff in VB.NET? I somehow didn't find the Random object?

 

Is there a good method to use or am I just not looking right :-(

My website
  • *Experts*
Posted

I use the Object browser's Search button. Press Ctrl-Alt-J for the object browser (or reassign to F2 like it was in VB6 :)), then press the little binocular button. I've found that between that and scouring the built-in MSDN help, I find a ton of useful stuff.

 

Plus, working in a team that's been using .NET for almost a year (in production, more if you count playing around), we share a lot of useful tidbits.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...