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
  1. Home
  2. General Programming
  3. C#
  4. Creating a bool random value

Creating a bool random value

Scheduled Pinned Locked Moved C#
debuggingquestionlounge
3 Posts 2 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.
  • B Offline
    B Offline
    Blue_Skye
    wrote on last edited by
    #1

    I'm in need of creating 200 bool random value (0 & 1). I'm coding like this : Random rand = new Random() rand.Next(0,2) If i run in debug mode, the values it create is really random but if i don't debug, let it run automatic, all the values is just 0 or 1. Can someone tell me why and give me the direction to solve it? Thanks!

    S 1 Reply Last reply
    0
    • B Blue_Skye

      I'm in need of creating 200 bool random value (0 & 1). I'm coding like this : Random rand = new Random() rand.Next(0,2) If i run in debug mode, the values it create is really random but if i don't debug, let it run automatic, all the values is just 0 or 1. Can someone tell me why and give me the direction to solve it? Thanks!

      S Offline
      S Offline
      S Senthil Kumar
      wrote on last edited by
      #2

      If you look closely at the documentation for the Random class, you will see that it doesn't work when calls to Next are made in quick succession. That explains your debug vs non-debug behavior. Instead of generating a random number between 0 and 2, wouldn't it be better if you generated random numbers (0 to size of int) and then converted them to binary? You can generate as many random numbers as you want until you get 200 binary digits. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

      B 1 Reply Last reply
      0
      • S S Senthil Kumar

        If you look closely at the documentation for the Random class, you will see that it doesn't work when calls to Next are made in quick succession. That explains your debug vs non-debug behavior. Instead of generating a random number between 0 and 2, wouldn't it be better if you generated random numbers (0 to size of int) and then converted them to binary? You can generate as many random numbers as you want until you get 200 binary digits. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        B Offline
        B Offline
        Blue_Skye
        wrote on last edited by
        #3

        Thanks! Your direction really works! :)

        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