none
Does RandBetween in microsoft word exist? RRS feed

  • Question

  • That's pretty much the question. I'm trying to get a code that chooses a random number between -1 and 1. 

    • Edited by tonayie Friday, August 3, 2012 3:16 AM
    Thursday, August 2, 2012 2:11 AM

Answers

  • Try:

    Randomize Timer
    h = CLng(Int((3 * Rnd()) - 1))

    Note that you may get an error if i=1 and h = -1.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by tonayie Friday, August 3, 2012 2:10 AM
    Friday, August 3, 2012 1:51 AM

All replies

  • There is no such function built in, but you can easily create on yourself:

    Function RandBetween(Bottom As Long, Top As Long) As Long
        RandBetween = Bottom + Int(Rnd * (Top - Bottom + 1))
    End Function

    and use it like the Excel function.

    Regards, Hans Vogelaar

    Thursday, August 2, 2012 5:22 AM
  • i used one from here: http://msdn.microsoft.com/en-us/library/f7s023d2(v=vs.90).aspx

    unfortunately, it does nothing for my code.




    • Edited by tonayie Friday, August 3, 2012 3:16 AM code is private
    Thursday, August 2, 2012 11:27 PM
  • Try:

    Randomize Timer
    h = CLng(Int((3 * Rnd()) - 1))

    Note that you may get an error if i=1 and h = -1.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by tonayie Friday, August 3, 2012 2:10 AM
    Friday, August 3, 2012 1:51 AM
  • Try:

    Randomize Timer
    h = CLng(Int((3 * Rnd()) - 1))

    Note that you may get an error if i=1 and h = -1.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    that's amazing, how did you know? it worked very well, perfectly and I've been trying for so long too. where's the negative value? so so amazing, thank you so much.
    • Marked as answer by tonayie Friday, August 3, 2012 2:10 AM
    • Unmarked as answer by tonayie Friday, August 3, 2012 2:10 AM
    Friday, August 3, 2012 2:10 AM
  • Hi tonayie,

    3 * Rnd() produces a number between 0 and 3.
    Int(3 * Rnd())  discards the decimal parts and thus returns 0, 1 or 2
    Deducting 1 from 0, 1 or 2 returns -1, 0 or 1.

    Quite simple, really.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    Friday, August 3, 2012 3:41 AM