Go Back   CodingForums.com > :: Computing & Sciences > Computer Programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-30-2003, 05:09 PM   PM User | #1
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
random number generator, c++

Can anyone see why this always shows the random number as 99?

PHP Code:
#include <iostream>
#include <windows.h>

int main(){
    
int x 1;
    
int y 99;
    
std::cout << "A random number between " << << " and " << << " is: " << rand()%x+y;
    return 
0;

__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Old 03-30-2003, 05:25 PM   PM User | #2
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
You need to seed it:

srand(time(NULL))

Be sure to include time.h
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 03-30-2003, 07:29 PM   PM User | #3
willie
New to the CF scene

 
Join Date: Mar 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
willie is an unknown quantity at this point
Try rand()%(x+y). It will work better!
willie is offline   Reply With Quote
Old 03-30-2003, 09:12 PM   PM User | #4
Nightfire
Senior Coder

 
Nightfire's Avatar
 
Join Date: Jun 2002
Posts: 4,266
Thanks: 6
Thanked 48 Times in 48 Posts
Nightfire is on a distinguished road
Thanks Not only did the (x+y) work better, but it was the only way it would work
__________________
Blue Panda
Website Design | 1 Pound Ads | 'ow much? | Coding Geeks
Nightfire is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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

Forum Jump


All times are GMT +1. The time now is 06:26 PM.


Advertisement
Log in to turn off these ads.