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. Random number

Random number

Scheduled Pinned Locked Moved C#
lounge
5 Posts 4 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.
  • I Offline
    I Offline
    ImanMahmoud
    wrote on last edited by
    #1

    i want to generate two random numbers , assume: Random rd = new Random(); int m1 = rd.Next(1,x); int m2 = rd.Next(1,y); i want to generate them for multiple times provided that the two numbers m1 & m2 do not repeated. thanks

    S C I 3 Replies Last reply
    0
    • I ImanMahmoud

      i want to generate two random numbers , assume: Random rd = new Random(); int m1 = rd.Next(1,x); int m2 = rd.Next(1,y); i want to generate them for multiple times provided that the two numbers m1 & m2 do not repeated. thanks

      C Offline
      C Offline
      Cyric74
      wrote on last edited by
      #2

      Do you mean you want to keep generating them until they are not equal to each other? Or you're going to do something like fill an array up with random numbers but you don't want any to be the same as another number in the array? If you're wanting to generate them until they aren't equal just a simple while loop will work: Random rd = new Random(); int m1 = rd.Next(1,x); int m2 = rd.Next(1,y); int iInfLoopChk = 0; //prevents while loop from locking your program in infinite loop while( m1 == m2 && iInfLoopChk < 5000 ) { m1 = rd.Next(1,x); m2 = rd.Next(1,y); iInfLoopChk++; } ^You'll need some logic if you're worried about the infinite loop that tells you both numbers cannot be anything other than equal to eachother. (Like if m1 is always 0 and m2 is always 0 due to logic error or special circumstance)

      1 Reply Last reply
      0
      • I ImanMahmoud

        i want to generate two random numbers , assume: Random rd = new Random(); int m1 = rd.Next(1,x); int m2 = rd.Next(1,y); i want to generate them for multiple times provided that the two numbers m1 & m2 do not repeated. thanks

        S Offline
        S Offline
        S Sansanwal
        wrote on last edited by
        #3

        Try Random rd = new Random(); int m1 = rd.Next(1, x); int m2; do { m2 = rd.Next(1, y); }while(m1 != m2); Sanjay Sansanwal www.sansanwal.com

        1 Reply Last reply
        0
        • I ImanMahmoud

          i want to generate two random numbers , assume: Random rd = new Random(); int m1 = rd.Next(1,x); int m2 = rd.Next(1,y); i want to generate them for multiple times provided that the two numbers m1 & m2 do not repeated. thanks

          I Offline
          I Offline
          ImanMahmoud
          wrote on last edited by
          #4

          Not that. The case of m1 = m2 is not a problem, but i want the pair of two numbers (m1 and m2 together) not to be repeated. for example: if x=2 and y=3, this pair of x and y not to be repeated, because i want to use them as one unit.

          L 1 Reply Last reply
          0
          • I ImanMahmoud

            Not that. The case of m1 = m2 is not a problem, but i want the pair of two numbers (m1 and m2 together) not to be repeated. for example: if x=2 and y=3, this pair of x and y not to be repeated, because i want to use them as one unit.

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            struct XY {
            int X, Y;
            }

            now just dump that in a hashtable, and check if the valuetype is in there. Note this will only work for ordered pairs. You will need to override gethascode and equals if u want them to be equal unordered or whichever way you fancy. hth :) top secret
            Download xacc-ide 0.0.3 now!
            See some screenshots

            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