Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Visual Basic
  4. VB 2005 RANDOM WITH PERCENTAGE

VB 2005 RANDOM WITH PERCENTAGE

Scheduled Pinned Locked Moved Visual Basic
tutorialquestionlounge
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    suguimoto
    wrote on last edited by
    #1

    Hello there, I would like to know if it´s possible to randomly pick a number based on a percentage, for example: from 1 to 5. nº % 1 10% 2 20% 3 10% 4 20% 5 40% I have 40% to pick 5 instead of the others. Is it possible to be implemented in VB2005 ?

    L T 2 Replies Last reply
    0
    • S suguimoto

      Hello there, I would like to know if it´s possible to randomly pick a number based on a percentage, for example: from 1 to 5. nº % 1 10% 2 20% 3 10% 4 20% 5 40% I have 40% to pick 5 instead of the others. Is it possible to be implemented in VB2005 ?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, Of course it is possible. Here is the simplest approach: - allocate an array of 100 elements; - now fill it according to your specs (10 ones, 20 twos, 10 threes,...); order does not matter. - now get a random number from Random in the range [0,99]; - use it as an index into the array, and get the element. Obviously, you do not really need the array, you could replace it with a couple of conditionals, in pseudo-code:

      // note: the thresholds are cumulative
      if num<10 then return 1
      if num<30 then return 2
      if num<40 then return 3
      if num<60 then return 4
      return 5

      :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


      1 Reply Last reply
      0
      • S suguimoto

        Hello there, I would like to know if it´s possible to randomly pick a number based on a percentage, for example: from 1 to 5. nº % 1 10% 2 20% 3 10% 4 20% 5 40% I have 40% to pick 5 instead of the others. Is it possible to be implemented in VB2005 ?

        T Offline
        T Offline
        T2102
        wrote on last edited by
        #3

        The easiest general solution for a very small # of discrete states is to invert the CDF. Generate a uniform on [0,1]. When it's between [0, .1) map to 1, [.1, .3) map to 2, [.3, .4) map to 3, [.4, .6) map to 4, [.6, 1) map to 5.

        L 1 Reply Last reply
        0
        • T T2102

          The easiest general solution for a very small # of discrete states is to invert the CDF. Generate a uniform on [0,1]. When it's between [0, .1) map to 1, [.1, .3) map to 2, [.3, .4) map to 3, [.4, .6) map to 4, [.6, 1) map to 5.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          And how is that any different from what I posted? :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


          T 1 Reply Last reply
          0
          • L Luc Pattyn

            And how is that any different from what I posted? :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            T Offline
            T Offline
            T2102
            wrote on last edited by
            #5

            It's very similar, but most likely is less efficient given the implementation of Random. (Yes I have designed several simulation algorithms before). Random generators are typically designed to generator random unsigned longs of uniforms efficiently. The generators that give you a random integer have an unnecessary multiplication thrown unless of course they are specialized for powers of two where they use bitwise operators.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups