Re: Random Numbers are NOT random !!!

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Gerry O'Brien [MVP] (gerry)
Date: 09/29/04


Date: Wed, 29 Sep 2004 08:51:22 -0300

You must call Randomize first in order to get a random seed value.

Rnd will always return the same numbers every time you run it unless you
provide it a different seed to start with each time. That is what Randomize
does. Just place it as the first line in your method before Rnd().

You can use Randomize(numbero of your choosing), or you can simply call
Randomize and it will pull the seed value from the system timer.

Have fun!

-- 
Gerry O'Brien [MVP]
Visual Basic .NET(VB.NET)
"mark4asp" <mark4asp#killspam#@ntlworld.com> wrote in message 
news:qk5ll0de8ah1l7ckvnk41kplskfaqpjhlc@4ax.com...
> The result of RandList() is a list of numbers which are all the SAME.
>
> Changing the d() function to the one which is commented out has no
> effect (of course).
>
> How can I get the function [ d() ] to return a different random number
> each time?
>
> Function RandList()
> Dim str As String = ""
> Dim i As Integer
> For i = 1 To 100
> str += d(10).toString() & ", "
> Next
> Return str
> End Function
>
> Function d(ByVal high) As Integer
> Return New System.Random().Next(1, high+1)
> End Function
>
> '  Function d(num As Integer)
> ' Dim RanNum As New Random
> ' Return RanNum.Next(1, num+1)
> '  End Function
> 


Relevant Pages

  • Re: randomize -- why?????
    ... Private Sub CreateRandomData() ... Dim i As Integer ... Cells.Value = Rnd ... But when I use "Randomize" once in a procedure, ...
    (comp.lang.basic.visual.misc)
  • Re: VB-6
    ... Debug.Print Rnd ... for the Randomize function. ... function **after** seeding the Randomize function took care of forcing ... the repeated sequences. ...
    (microsoft.public.vb.general.discussion)
  • Re: Creating a button...
    ... Private Sub CommandButton1_Click ... Dim rand_dice As Double ... rand_dice = Rnd() ... Mark, just to make things better, please add randomize prior to the ...
    (microsoft.public.excel.misc)
  • Re: how to generate random string?
    ... Documentation says that Randomize seeds the Rnd function with a value from ... The Timer function returns the number of seconds since ... is why they limit the LCG to 24 bits. ... I have not figured out what the Randomize function does. ...
    (microsoft.public.scripting.vbscript)
  • Re: Text as argument to Randomize function
    ... just after calling Rnd with a negative number will produce ... repeatable sequences. ... Yes but -- what I said was that Randomize is unnecessary, ... every time just by using Rnd -x without Randomize. ...
    (microsoft.public.vb.general.discussion)