Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-13-2008, 09:28 PM
hellochar hellochar is offline
Newbie
 
Join Date: Jul 2008
Posts: 10
Rep Power: 0
hellochar is on a distinguished road
Question rand() always returns 0.000

Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main(void) {
    int a = 0;
    printf("GNU GCC compiler RAND_MAX: %i \n", RAND_MAX);
    time_t seed;
    time(&seed);
    srand((int) seed);
    printf("Seed is : %i \n", seed);
    for(a = 0; a < 20; a++) {
        printf("rand: %0.3f  \n", rand());
    }
    getchar();
}
Using Code::Blocks for my IDE, with the GNU GCC Compiler ported through MinGW. The code says that RAND_MAX is 32767, and the seed is some arbitrary number, but rand will always return 0.000. Any ideas? I'm thinking that the compiler didn't implement rand(), but that makes no sense at all considering how widely used it is.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-13-2008, 09:44 PM
Aereshaa's Avatar   
Aereshaa Aereshaa is offline
Guru
 
Join Date: Apr 2008
Posts: 539
Rep Power: 9
Aereshaa is a jewel in the roughAereshaa is a jewel in the roughAereshaa is a jewel in the roughAereshaa is a jewel in the rough
Default Re: rand() always returns 0.000

Unix specification says that rand() returns int, not double. Here. You are currently displaying it as a double-precision floating-point.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-13-2008, 10:39 PM
MeTh0Dz MeTh0Dz is offline
SLICE OWNZ YOUR SOUL
 
Join Date: May 2008
Posts: 294
Last Blog:
Ternary Operator CPP
Rep Power: 0
MeTh0Dz has a spectacular aura aboutMeTh0Dz has a spectacular aura aboutMeTh0Dz has a spectacular aura about
Default Re: rand() always returns 0.000

Also it's not necessary to do all that with time.

Just do this...

Code:
srand(time(NULL));
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-13-2008, 11:39 PM
hellochar hellochar is offline
Newbie
 
Join Date: Jul 2008
Posts: 10
Rep Power: 0
hellochar is on a distinguished road
Default Re: rand() always returns 0.000

oh... that was stupid of me. thanks guys, 's working now
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Tags
gcc, mingw, rand



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Function returns nothing. shibbythestoner PHP Forum 14 12-21-2007 04:20 PM
responseXML returns null plits AJAX 1 10-30-2007 07:48 AM


All times are GMT -5. The time now is 10:51 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads