473,396 Members | 1,683 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generate a random number question

Another question,my teacher gave me a code for

generate a random number from 1 - range, but I can't made it work, where is
the problem?

Thanks!!

code:

#include <math.h>

unsigned int RandomNumber(int range) {

static int seed = 1;

srand(seed);

seed++;

return (rand() % range) + 1;

}
~ Let us linux ~
-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 22 '05 #1
4 2372
Wahoo wrote:

Another question,my teacher gave me a code for

generate a random number from 1 - range, but I can't made it work, where is
the problem?


Define: can't made it work

The only thing that can be seen immediatly, is:
Don't seed the random number generator more then once.
In other words: call srand only once in your program. Typically
this is done in main, at the very beginning.

int main()
{
srand( time( 0 ) );

...

}

If you seed the random number generator yourself all the
time, you are actually doing harm to the quality of the
generator. 'Randomness' is a statistical property of a *sequence*
of numbers. You need to let the generator work on it's own in order
to build up an undisturbed (quasi-)random sequence.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #2

"Wahoo" <wa***@yahoo.com> wrote in message
news:40********@lungfunggdn.org...
Another question,my teacher gave me a code for

generate a random number from 1 - range, but I can't made it work, where is the problem?

Thanks!!

code:

#include <math.h>

unsigned int RandomNumber(int range) {

static int seed = 1;

srand(seed);

seed++;

return (rand() % range) + 1;

}


You need a better teacher, that function is rubbish.

The C FAQ has a section on generating random numbers.

http://www.eskimo.com/~scs/C-faq/s13.html questions 13.15 to 13.20. 13.16
and 13.17 explain why your teachers function is poor.

john
Jul 22 '05 #3
Here are some things to think about.

Wahoo wrote:
#include <math.h>
rand() and srand() aren't in math.h header but in stdlib.h. This is
probably why the code doesn't even compile for you. Also check up on
the "new" way of including the C header files (cmath and cstdlib).
unsigned int RandomNumber(int range) {
Hmmm. Just what does it mean to return an unsigned number but have a
signed range? What SHOULD happen if "range" is 0? Or if it is negative?
static int seed = 1;
srand(seed);
seed++;
You set the seed for the generator every time you enter the function to
one more than what it was set to the previous time. Why? You might
want to read about what seeds are used for.
return (rand() % range) + 1;
See above and think about what will happen for 0 or negative values for
"range".
}

Jul 22 '05 #4
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2g************@uni-berlin.de>...

[ ... ]
The C FAQ has a section on generating random numbers.

http://www.eskimo.com/~scs/C-faq/s13.html questions 13.15 to 13.20. 13.16
and 13.17 explain why your teachers function is poor.


The "solutions" presented in the FAQ provide little improvement.

If rand() is at all competently written, several of the least
significant bits have already been eliminated, so there's little real
difference between using % and using / to clamp the output to a range.

Unfortunately, both result in biased results except when RAND_MAX
happens to be an even multiple of the range you're asking for. In the
common case that RAND_MAX is a prime number, that's a pretty rare
occurrence...

Code like this can produce unbiased results:

int rand_lim(int limit) {
/* return a random number between 0 and limit inclusive.
*/

int divisor = RAND_MAX/(limit+1);
int retval;

do {
retval = rand() / divisor;
} while (retval > limit);

return retval;
}

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: vishal | last post by:
i want to generate a random number of a fixed length so how can i do this ?? i know some function which returns a single random character at a time but is there any built-in function which...
2
by: Laphan | last post by:
Hi All This is a strange request, but I just cannot fathom how to do it. In theory the requirement is very basic, but in practise its a noodle!! I have 10 team names like so: Team A Team...
18
by: Toby Newman | last post by:
I need to randomly choose one of four paths in my program. Using the tools I know, the best way I can think to do it is by doing something like the following: //==============================...
15
by: John Cassidy | last post by:
This has been driving me crazy. I've done basic C in school, but my education is mainly based on object oriented design theory where Java is our tool. For some reason, while helping a friend with a...
2
by: Henry | last post by:
Hi, How can I generate an eight digit random? Can I use the staff name to generate it? May I ask is there any sample c# code to see? Thanks
12
by: Jim Michaels | last post by:
I need to generate 2 random numbers in rapid sequence from either PHP or mysql. I have not been able to do either. I get the same number back several times from PHP's mt_rand() and from mysql's...
15
by: Orchid | last post by:
Hello, I am looking to generate a unique ID field on MS. Access. The ID is with 10 digits with the combination of 5 Letters from the 26 letters and 5 Numbers from 1 to 9. The letters and numbers...
5
by: scan87 | last post by:
Can somebody please help me with the following problem. I need to submit the problem on Monday. A program is required which could be used to help a child practice their multiplication tables. The...
20
by: jjmillertime | last post by:
I'm new so i apologize if this is in the wrong spot. I'm also new to programming in C and i've been searching for quite a while on how to create a program using C that will generate two random...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.