Tech Support > Computers & Technology > Visual Basic question
Visual Basic question
Posted by Tom on August 10th, 2004


I know that this isn't the prime group for this question, but the VB groups
seem seem slow...3 messages a month. I just got the beginners package
(it's old, version 5.0). I followed the directions on the first program,
Lucky 7,
and it turned out OK, except for one peculiar detail. When you hit the Spin
button (command 1 button), the numbers are not random. It plays the exact
same sequence from game to game. Here is the code for Command 1.

Private Sub Command1_Click()
Image1.Visible = False
Label1.Caption = Int(Rnd * 10)
Label2.Caption = Int(Rnd * 10)
Label3.Caption = Int(Rnd * 10)
If (Label1.Caption = 7) Or (Label2.Caption = 7) Or (Label3.Caption = 7) Then
Image1.Visible = True
Beep
End If

End Sub

Can you see anything wrong here? The game plays, it just isn't *random*.
Thanks,
-Tom


Posted by Tom on August 10th, 2004



Sorry, I didn't mean "game to game", I meant that whenever you close the
game
and start it again, the same sequence of numbers (sets of 3) appear every
time
you hit spin. I.E. the first three games (spins) always has a 7, and so
on...
-Tom


"Tom" <tom@wallyworld.net> wrote in message
news:10hg84a1e4ggr50@corp.supernews.com...


Posted by Unk on August 10th, 2004


On Mon, 9 Aug 2004 18:17:19 -0700, "Tom" <tom@wallyworld.net> wrote:

You need to randomize the generator with the "Randomize" command
See the above 3rd line

Posted by Unk on August 10th, 2004


You're welcome.

On Mon, 9 Aug 2004 19:07:55 -0700, "Tom" <tom@wallyworld.net> wrote:

Posted by Tom on August 10th, 2004



That did it. Thanks.
-Tom


"Unk" <not@aol.com> wrote in message
news:nbagh019khpdn66ufale931b7vq9tdev12@4ax.com...


Posted by Duane Arnold on August 10th, 2004


"Tom" <tom@wallyworld.net> wrote in
news:10hg8emk14md16e@corp.supernews.com:

..Caption is treated as text or string and I think that's your problem.

I did your project with VB.NET and there is no Label.Caption anymore it
is Label.Text now.

I got different random numbers each time I pressed the button.

You'll find that in some of the training books that examples are wrong
sometimes.

Duane



Posted by WormWood on August 10th, 2004


Hey! Is 24hoursupport.helpdesk a Prime Group now? Secret: Experts hang
out here, dispensing pure magic at times, like now. It was good to see
someone take the time to post back with their appreciative comments :-)


"Tom" <tom@wallyworld.net> wrote in message
news:10hgb37q2b3pg12@corp.supernews.com...


Posted by Atreju on August 10th, 2004


On Mon, 9 Aug 2004 18:17:19 -0700, "Tom" <tom@wallyworld.net> wrote:

First of all, I don't know what NGs you've been looking at, but the
ones I regularly participate in are filled with hundreds of new posts
every day.

Check out:

comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion

I will address your current issue below:

This was recently discussed in one of the NGs I mentioned.
The problem is the Rnd function is not quite what you'd expect. In
order to randomize the seed by which VB starts the Rnd function, you
must first call a statement in your program.

May as well call it in your Form_Load event, this way it will be taken
care of and out of the way.

The statement is called "Randomize"
Yep, that's it!

Simply put that word into your Form_Load event and your Rnd functions
will all be more truly random.

Also, if you'd like an even better randomization, then Rnd your Rnd!

Eg: Text1.Text = Int((Rnd * 10) * Rnd())
This way you have really seeded it randomly.
There are many more methods to accomplish a more random number.
Do a google search in one of the aforementioned groups for it.

Best luck.

And come over to the VB6 groups for such questions. There are TONS of
people there to help you :-)



---Atreju---

Posted by Tom on August 10th, 2004



....and thanks once again! I will check out that group. I have a question on
discarding numbers once they have been randomly selected, for the purpose
of a lottery #'s generator:-)
-Tom

"Atreju" <someone@who.hates.junkmail> wrote in message
news:g9dgh0p45vv1j7rb1ip6bemqhum1989tdq@4ax.com...


Posted by Kieran Simkin on August 10th, 2004


"Atreju" <someone@who.hates.junkmail> wrote in message
news:g9dgh0p45vv1j7rb1ip6bemqhum1989tdq@4ax.com...
Bad advice, seed a random number generator (RNG) once per-program and
request the smallest amount of random data you need to complete your task
(do not call the random function more than once to achieve more
"randomness"). Think about it, if randomising (again) a random number
produced more randomness, surely this would be built into the random number
generator itself? In truth, doing stuff like this actually reduces the
randomness of a number for reasons that are far too complex to be explained
here.

Just a pedantic point, but I've dealt with other people's code in the past
that has fallen into this trap and it's caused me some problems.



Posted by Atreju on August 10th, 2004


On Tue, 10 Aug 2004 04:38:01 GMT, "Kieran Simkin"
<kieran@digital-crocus.com> wrote:
SNIP

I dunno, worked really well for me.
I suppose perhaps it adds some redundnacy that is not necessary, but I
found even better results by doing that.


---Atreju---

Posted by John H. Guillory on August 16th, 2004


On Mon, 9 Aug 2004 18:17:19 -0700, "Tom" <tom@wallyworld.net> wrote:

Basic, and wanted to make 100% sure, before I answered you... I also
just got through re-installing everything on my computer, due to a
recent crash caused by viruses.... Anyway, to make a long story quite
short.... In the Form Load event, add 1 command "Randomize" without
quotes

I think you could probably use X = RND(-1 * Timer) as well, as most
of the time Randomize just does a random function from -timer....


Posted by Brian on August 16th, 2004


Tom wrote:

Subscribe to comp.lang.basic.visual.misc -- it's not slow at all.





Similar Posts