Consulting

Results 1 to 5 of 5

Thread: Making the random...random

  1. #1
    VBAX Regular
    Joined
    Apr 2010
    Posts
    21
    Location

    Making the random...random - SOLVED

    During a lecture I click on a button on a slide and a name is randomly picked from a list and displayed on the screen. Below is the code. The problem is that the random selection is always in the same order every time I start the slide presentation. This might be how the random is seeded? I have about 100 student names in the text file.

    Is there a way to make the seed random, so the order is always different each time the presentation starts.

    Thanks.

    ----------------------------

    Private Sub UserForm_Initialize()

    Me.StartUpPosition = 0 'From Top Left
    Me.Top = 310
    Me.Left = 450

    Dim arr, fs, a
    Dim ThePath As String
    Dim NewPath As String
    Dim name As String

    ThePath = CurDir ' Returns Path of lecture
    NewPath = ThePath & "\names\names.txt" ' Creates a new path where names are located
    Set fs = CreateObject("Scripting.FileSystemObject") ' Chooses a random student from text file
    Set a = fs.openTextFile(NewPath)
    arr = Split(a.readall, Chr(13) & Chr(10))
    name = arr(Int((112 * Rnd)))
    lblName.Caption = name ' displays name in label caption
    End Sub

    Private Sub cmdOK_Click()

    Dim SlideNumber As Long
    Unload frmName ' unloads form from memory

    End Sub
    Last edited by Waubain; 09-10-2010 at 10:16 AM. Reason: problem solved

  2. #2
    VBAX Contributor
    Joined
    May 2008
    Posts
    198
    Location
    Try something like this to see if it make a difference:
    [vba]Rnd(cSng(Time))[/vba]

    This should give you a different number each time

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,730
    Location
    Easiest way I know is to use Randomize

    From Help:

    Randomize Statement

    Initializes the random-number generator.

    Syntax

    Randomize [number]

    The optional number argument is a Variant or any valid numeric expression.

    Remarks

    Randomize uses number to initialize the Rnd function's random-number generator, giving it a new seed value. If you omit number, the value returned by the system timer is used as the new seed value.

    If Randomize is not used, the Rnd function (with no arguments) uses the same number as a seed the first time it is called, and thereafter uses the last generated number as a seed value.


    Note
    To repeat sequences of random numbers, call Rnd with a negative argument immediately before using Randomize with a numeric argument. Using Randomize with the same value for number does not repeat the previous sequence.
    Paul

  4. #4
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    [VBA]Randomize
    name = arr(Int((112 * Rnd)))[/VBA]

    Should do it
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

  5. #5
    VBAX Master
    Joined
    Feb 2007
    Posts
    2,094
    Location
    Your formula can return zero by the way, I'm assuming that arr is an array that has a zero member?

    Also this may interest you http://www.pptalchemy.co.uk/Random_Choice.html
    John Wilson
    Microsoft PowerPoint MVP
    Amazing Free PowerPoint Tutorials
    http://www.pptalchemy.co.uk/powerpoi...tutorials.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •