Computer Forums

Member Login

Remember Me? Sign Up! | Forgot Password
 
Slogan
 
Computer Forums > Programmers Lounge > Programming Discussions » C - Random Number Generator!
Closed Thread
Old 08-26-2008, 09:10 PM   #1 (permalink)
 
Newb Techie

Join Date: Aug 2008

Posts: 16

george_1988 is on a distinguished road

Default C - Random Number Generator!

Hello,

I'm somewhere between beginner and intermediate when it comes to writing C code and really need a random number generator for a small project I have been working on.

I've been searching for ages and can't find any WORKING code to get a random number generator!

If anybody could help me out with this I would appreciate it very much. I am looking for a generator that is seeded by the clock because I want a different number everytime the program runs. Note: I am looking for C, not C++.

Thank you!

Last edited by george_1988; 08-26-2008 at 09:14 PM.
george_1988 is offline  
Old 08-26-2008, 11:15 PM   #2 (permalink)
 
Monster Techie

Join Date: May 2004

Location: /usr/root/mn/us

Posts: 1,121

bla!! is on a distinguished road

Default Re: C - Random Number Generator!

Couldn't you just call srand(time(0)) before you call rand()

This would seed the rand function with the current system time instead of using the default value of 1 for the seed.
__________________
<br>
Its a frigging Laptop, not a Labtop!!!!
bla!! is offline  
Old 08-31-2008, 04:27 AM   #3 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,418

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: C - Random Number Generator!

Technically speaking there is no true random number generator. We studied it for a month in my C++ Advanced Algorithms course in college . You could however use srand like bla said and use a null value for time.

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

int main()
{

srand(time(NULL));
printf("%d", rand() % 10000);

}
Something like that will generate a number out of 10000 each time you run it. Just put it in a for loop to run it X amount of times.
__________________

Baez is offline  
Old 09-05-2008, 09:34 PM   #4 (permalink)
 
Newb Techie

Join Date: Aug 2008

Posts: 8

spoonmeatloa is on a distinguished road

Default Re: C - Random Number Generator!

Just use a vbs heres a code that i use somethimes:
Randomize()
Number = Int(HIGHESTnumberYOUwant * rnd())
call msgbox("Random Number" & Number,16,"Random Num")
spoonmeatloa is offline  
Old 09-06-2008, 12:12 AM   #5 (permalink)
Baez's Avatar
 

Join Date: Sep 2005

Location: Toronto, Canada

Posts: 5,418

Baez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of lightBaez is a glorious beacon of light

Default Re: C - Random Number Generator!

The thread title says C not VB. lol... cvb.
__________________

Baez is offline  
 
Closed Thread

Thread Tools
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Find A Cell Phone Number Osiris Phones – PDA’s – Bluetooth – Other handhelds 0 08-25-2008 10:19 AM
Increase number of simultaneous downloads superdave1984 Browser & General Internet Questions 0 06-12-2008 10:55 PM
Random Generator? akasixcon Programming Discussions 1 03-19-2008 03:11 AM
random number funkywaggnelz Off Topic Discussion 4 02-26-2008 06:41 PM
Random and Frequent Restarts with a BSOD Nick Hardware Troubleshooting 2 02-01-2008 10:12 AM