Results 1 to 3 of 3

Thread: rand() Question.

  1. #1
    Loves duck, Peking Duck! bsodmike's Avatar
    Join Date
    Jul 2003
    Location
    Colombo
    Posts
    2,909
    Thanks
    495
    Thanked
    92 times in 80 posts
    • bsodmike's system
      • Motherboard:
      • Misc.
      • CPU:
      • Mac Pro 2.8 GHz (8-cores) / iMac 2.8GHz C2D Extreme Edition / MacBook Pro 15" 2.33 & 13.3" 2.26 GHz
      • Memory:
      • 2GB DDR2 kits on all systems / FB ECC RAM in Mac Pro
      • Storage:
      • 320GB + 3x 1TB, Mac Pro / 500 GB, iMac / 2x 1TB & 2x 2TB WD My Book Studio II, via FW800 to Mac Pro
      • Graphics card(s):
      • ATI Radeon HD 2600 XT 256MB (two dual-link DVI ports / Mac Pro)
      • PSU:
      • Misc.
      • Case:
      • Misc.
      • Operating System:
      • Mac OS X 10.5 (Mac Pro) / Mac OS X 10.4.11 (others)
      • Monitor(s):
      • Apple 23" Cinema HD / Dell FPW2408 / iMac 24" display
      • Internet:
      • 2mbps ADSL (Mac Pro) / 512kbps WiMax (Wifi/MacBook Pro)

    rand() Question.

    Hiyas,

    Got a question about the rand() function. If I want to generate a random number between a range of say 0 - x how would this function look like?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
     
    #define SEED 12345
    
    main() 
    {
    
        float x;
        int n;
    
            srand(SEED);
                x = rand() / RAND_MAX;
                n = 1 + (int) (6 * x);
    
          printf("x = %d", n);
    }
    I'm using the gcc/g++ compiler but it doesn't seem to know what RAND_MAX is (which I believe is the largest random number possible). Of course I will include time.h and based the seed on time, i.e. srand(time);

    Update: Just found the solution I believe - http://www.cprogramming.com/tutorial/random.html

    Cheers, Mike
    Last edited by bsodmike; 20-02-2006 at 05:32 AM.

  2. #2
    Theoretical Element Spud1's Avatar
    Join Date
    Jul 2003
    Location
    North West
    Posts
    7,508
    Thanks
    336
    Thanked
    320 times in 255 posts
    • Spud1's system
      • Motherboard:
      • Gigabyte Aorus Master
      • CPU:
      • 9900k
      • Memory:
      • 16GB GSkill Trident Z
      • Storage:
      • Lots.
      • Graphics card(s):
      • RTX3090
      • PSU:
      • 750w
      • Case:
      • BeQuiet Dark Base Pro rev.2
      • Operating System:
      • Windows 10
      • Monitor(s):
      • Asus PG35VQ
      • Internet:
      • 910/100mb Fibre
    many ways to do it , although ofc you cannot get a truely random number out of a computer That link should be more than enough for most needs though, i've added it to my bookmarks ty

  3. #3
    Loves duck, Peking Duck! bsodmike's Avatar
    Join Date
    Jul 2003
    Location
    Colombo
    Posts
    2,909
    Thanks
    495
    Thanked
    92 times in 80 posts
    • bsodmike's system
      • Motherboard:
      • Misc.
      • CPU:
      • Mac Pro 2.8 GHz (8-cores) / iMac 2.8GHz C2D Extreme Edition / MacBook Pro 15" 2.33 & 13.3" 2.26 GHz
      • Memory:
      • 2GB DDR2 kits on all systems / FB ECC RAM in Mac Pro
      • Storage:
      • 320GB + 3x 1TB, Mac Pro / 500 GB, iMac / 2x 1TB & 2x 2TB WD My Book Studio II, via FW800 to Mac Pro
      • Graphics card(s):
      • ATI Radeon HD 2600 XT 256MB (two dual-link DVI ports / Mac Pro)
      • PSU:
      • Misc.
      • Case:
      • Misc.
      • Operating System:
      • Mac OS X 10.5 (Mac Pro) / Mac OS X 10.4.11 (others)
      • Monitor(s):
      • Apple 23" Cinema HD / Dell FPW2408 / iMac 24" display
      • Internet:
      • 2mbps ADSL (Mac Pro) / 512kbps WiMax (Wifi/MacBook Pro)
    Yea pseudorandom is all I'm after

    Cheers.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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