C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 14th, 2004, 01:30 AM
nCmdShow nCmdShow is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 3 nCmdShow User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Random number.

Hi,
I am writing a program and I just need a little help. I need my program to
generate a random number. Does anyone know how to do this? If so please
let me know.

Thanks.
-nCmdShow

Reply With Quote
  #2  
Old June 14th, 2004, 06:39 AM
_julien_ _julien_ is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 11 _julien_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
use these functions:

long
random(void);
void
srandom(unsigned long seed);


example:

int i;

srandom(time(0));
for (i = 0; i < 10; i++) printf("%u\n", random());

--
julien barbier

Reply With Quote
  #3  
Old June 14th, 2004, 01:36 PM
drizzle drizzle is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Location: OREGON
Posts: 24 drizzle User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
or you could use:

srand(GetTickCount()); //initialises the function each time with the computer tick count so it is a garenteed random number
int random = rand()% 6; //the 6 defines that it will give you a number between 0 and 6, you can change it to whatever you like

-drizzle

Reply With Quote
  #4  
Old June 15th, 2004, 12:17 AM
nCmdShow nCmdShow is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 3 nCmdShow User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile Thanks.

Thanks alot for all your help.

Reply With Quote
  #5  
Old July 1st, 2004, 10:21 PM
Monkey Junk Monkey Junk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 5 Monkey Junk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
To use a random number you use rand (); to generate a random number. Everytime you run the program, you will receive the same sequence of numbers, but if you seed the random number with ( time (0)), this will like the random generator to the internal clock. Be sure to include the <ctime> header file. Also, only call the srand function once in your program. Calling it more than once can give you problems.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Random number.


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT