random numbers

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • moni

    random numbers

    Hi,

    Can anyone tell me , how I would generate random numbers, for an
    interval say from 15 to 450 in C.

    Plz lemme know.

    thanx..

  • JohnGoogle@hotmail.co.uk

    #2
    Re: random numbers

    I just tried this example:

    class TestRandom
    {
    static void Main(string[] args)
    {
    Console.WriteLi ne("Testing Random class");

    Random rand = new Random();

    for (int i = 0; i < 100; i++)
    Console.WriteLi ne("Value {0}",rand.Next( 15,451));

    Console.WriteLi ne("\nPress ENTER to close");
    Console.ReadLin e();
    }
    }

    Random.Next expects a min and max value as parameters. Min is inclusive
    and max is exclusive (that's why I passed 15 and 451).

    Hope this helps.

    BTW. I found this (didn't know it before hand) by searching for random
    is the object browser. I find it exteremly useful when trying to find
    things that I don't know about.

    John.

    moni wrote:
    Hi,
    >
    Can anyone tell me , how I would generate random numbers, for an
    interval say from 15 to 450 in C.
    >
    Plz lemme know.
    >
    thanx..

    Comment

    • Michael  Goldfarb

      #3
      Re: random numbers

      The standard C functions do not have arguments for a range of values.
      You can however easily create a function to do this:

      int myRand(int min, int max)
      {
      srand(time(0));
      return (rand()%(max-min))+min;
      }

      On Oct 26, 1:48 pm, "moni" <mons.2...@gmai l.comwrote:
      Hi,
      >
      Can anyone tell me , how I would generate random numbers, for an
      interval say from 15 to 450 in C.
      >
      Plz lemme know.
      >
      thanx..

      Comment

      • JohnGoogle@hotmail.co.uk

        #4
        Re: random numbers

        Sorry.

        As it was a csharp newsgroup I thought the OP was wanting to know how
        to do it in C# / .NET. I now see that he said C.

        Comment

        • bwray314@gmail.com

          #5
          Re: random numbers


          Michael Goldfarb wrote:
          The standard C functions do not have arguments for a range of values.
          You can however easily create a function to do this:
          >
          int myRand(int min, int max)
          {
          srand(time(0));
          return (rand()%(max-min))+min;
          }
          >
          On Oct 26, 1:48 pm, "moni" <mons.2...@gmai l.comwrote:
          Hi,

          Can anyone tell me , how I would generate random numbers, for an
          interval say from 15 to 450 in C.

          Plz lemme know.

          thanx..
          To the OP:

          This works, but don't do copy/paste this into your code. In your code,
          be sure to call srand(time(0)) somewhere else (top of main() is good)
          or every single call to myRand() in the same clock second will return
          the same random number.

          And in case you weren't already aware, you'll also need to #include
          <time.hand <stdlib.hto have the time(), rand(), and srand()
          functions prototyped.

          Comment

          • moni

            #6
            Re: random numbers

            Thanx alot. I did that, and am getting random numbers,
            But is there a way by which I can get unique numbers?

            Thanks.


            bwray...@gmail. com wrote:
            Michael Goldfarb wrote:
            The standard C functions do not have arguments for a range of values.
            You can however easily create a function to do this:

            int myRand(int min, int max)
            {
            srand(time(0));
            return (rand()%(max-min))+min;
            }

            On Oct 26, 1:48 pm, "moni" <mons.2...@gmai l.comwrote:
            Hi,
            >
            Can anyone tell me , how I would generate random numbers, for an
            interval say from 15 to 450 in C.
            >
            Plz lemme know.
            >
            thanx..
            >
            To the OP:
            >
            This works, but don't do copy/paste this into your code. In your code,
            be sure to call srand(time(0)) somewhere else (top of main() is good)
            or every single call to myRand() in the same clock second will return
            the same random number.
            >
            And in case you weren't already aware, you'll also need to #include
            <time.hand <stdlib.hto have the time(), rand(), and srand()
            functions prototyped.

            Comment

            • Rick Lones

              #7
              Re: random numbers

              moni wrote:
              Thanx alot. I did that, and am getting random numbers,
              But is there a way by which I can get unique numbers?
              >
              Thanks.
              >
              Depends what you mean. If the generation is really random (or pseudo-random)
              there is no guarantee that the generated sequence would contain unique numbers.
              Is what you really want a random permutation (shuffle) of the integers from 15
              to 450?

              -rick-
              >
              bwray...@gmail. com wrote:
              >Michael Goldfarb wrote:
              >>The standard C functions do not have arguments for a range of values.
              >>You can however easily create a function to do this:
              >>>
              >>int myRand(int min, int max)
              >>{
              >> srand(time(0));
              >> return (rand()%(max-min))+min;
              >>}
              >>>
              >>On Oct 26, 1:48 pm, "moni" <mons.2...@gmai l.comwrote:
              >>>Hi,
              >>>>
              >>>Can anyone tell me , how I would generate random numbers, for an
              >>>interval say from 15 to 450 in C.
              >>>>
              >>>Plz lemme know.
              >>>>
              >>>thanx..
              >To the OP:
              >>
              >This works, but don't do copy/paste this into your code. In your code,
              >be sure to call srand(time(0)) somewhere else (top of main() is good)
              >or every single call to myRand() in the same clock second will return
              >the same random number.
              >>
              >And in case you weren't already aware, you'll also need to #include
              ><time.hand <stdlib.hto have the time(), rand(), and srand()
              >functions prototyped.
              >

              Comment

              • rossum

                #8
                Re: random numbers

                On 26 Oct 2006 11:45:11 -0700, "moni" <mons.2110@gmai l.comwrote:
                >Thanx alot. I did that, and am getting random numbers,
                >But is there a way by which I can get unique numbers?
                >
                >Thanks.
                Knuth, Volume II, Algorithm 3.4.2 P

                rossum

                Comment

                Working...