CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 1999
    Posts
    505

    Turbo C equivalent command in Visual C

    Hi,
    I am using visual C 6.0. I have a code in turbo which uses following command:
    Code:
    randomize();
    dx1=random(c2)-c3;
    where
    Code:
       int random (int num )
    returns a number between 0 and (num-1)

    The visual C code is:

    Code:
    int I = IMin + rand() % (IMax - IMin + 1);
    So its confusing. Can somebody guide me how to convert from Turbo C to Visual C??

    Zulfi.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Turbo C equivalent command in Visual C

    The 'standard' c function to obtain a pseudo-random number is rand(). It returns a pseudo-random number between 0 and RAND_MAX. See http://msdn.microsoft.com/en-us/library/398ax69y.aspx. Also see srand() to 'seed' the random number generator. If you want a number between a range then this needs to be programmed. See the example of producing a number within a range in the previous link (RangedRandDemo()).

    If you can use c++11 instead of c, there are several much-improved methods of obtaining random numbers available. See
    http://www.cplusplus.com/reference/random/
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Jun 1999
    Posts
    505

    Re: Turbo C equivalent command in Visual C

    Hi,
    Thanks for your response. Plz provide me the specific command to convert my TC code into VC. I have shown you the TC code. Plz show me how to convert it into VC?
    Hope to get a response for this.
    Zulfi.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Turbo C equivalent command in Visual C

    There is no specific command to convert from using Turbo-C random() to visual-c rand(). I suggest you create a function called random() that takes as an argument the required max value (which must be no greater than RAND_MAX (32767). Something like this
    Code:
    int random(int num)
    {
          return rand() % num;
    }
    You can also create a function called randomize() to replace the one you use. Something like
    Code:
    void randomize()
    {
         srand( (unsigned)time( NULL ) );
    }
    Note that you'll need to include time.h (and also stdlib.h if you aren't already).
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jun 1999
    Posts
    505

    Re: Turbo C equivalent command in Visual C

    Hi,
    Thanks. Good work.

    Zulfi.

  6. #6
    Join Date
    May 2004
    Location
    45,000FT Above Nevada
    Posts
    1,539

    Re: Turbo C equivalent command in Visual C

    Quote Originally Posted by Zulfi Khan2 View Post
    Hi,
    Thanks. Good work.

    Zulfi.

    Isn't this pretty much the same answer you got in this thread ??

    Please don't double post problems...

    And if you google srand(int) you will find 730,000 results and probably all with examples.

    http://forums.codeguru.com/showthrea...domize-%28-%29
    Jim
    ATP BE400 CE500 (C550B-SPW) CE560XL MU300 CFI CFII

    "The speed of non working code is irrelevant"... Of course that is just my opinion, I could be wrong.

    "Nothing in the world can take the place of persistence. Talent will not; nothing is more common than unsuccessful men with talent. Genius will not; unrewarded genius is almost a proverb. Education will not; the world is full of educated derelicts. Persistence and determination are omnipotent. The slogan 'press on' has solved and always will solve the problems of the human race."...Calvin Coolidge 30th President of the USA.

  7. #7
    Join Date
    Jul 2013
    Posts
    576

    Re: Turbo C equivalent command in Visual C

    Quote Originally Posted by Zulfi Khan2 View Post
    So its confusing. Can somebody guide me how to convert from Turbo C to Visual C??
    If you want to stick with C, rand() is fine. Here are a couple of references,

    http://www.eternallyconfuzzled.com/a..._art_rand.aspx

    http://www.cplusplus.com/articles/EywTURfi/

    But if randomness is important in your application and you're using C++ then you definately should consider <random>. It's part of the C++ 11 standard library so you'll have to upgrade. It's a little harder because it's more flexible and versatile but it's worth it.

  8. #8
    Join Date
    Jun 1999
    Posts
    505

    Re: Turbo C equivalent command in Visual C

    Hi,
    Thanks razzle for your update. Actually I am using VC6.0. I can install VS2010. Is random ( ) available in Visual Studio 2010??

    Zulfi.

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: Turbo C equivalent command in Visual C

    Quote Originally Posted by Zulfi Khan2 View Post
    Thanks razzle for your update. Actually I am using VC6.0. I can install VS2010. Is random ( ) available in Visual Studio 2010??
    No. Random() and randomize() are Turbo c specific library extensions - although you now have replacement functions for them. The <random> classes are part of c++11 and are included in VS2010. I would strongly recommend that you use VS2010 rather than VC6 if you have it.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured