473,902 Members | 5,194 Online

Random number between 1 and 4000?

I can't seem to control the rand() function. I just makes an evil
humongeous number. How do I make it generate random numbers from 1 -
4000??

Mar 28 '06
27 9811

"A. Sinan Unur" <1u**@llenroc.u de.invalid> writes:
"Ralph A. Moritz" <ra*******@gmai l.com> wrote in
news:11******** **************@ u72g2000cwu.goo glegroups.com:
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int rn;
srand(time(NULL ));
rn = rand() % 4000;

printf("%d\n", rn);
return 0;
}

There are two problems with that code:

1) The OP wanted numbers between 1 and 4000. The code above will yield
zero occasionally.

2) The lower order bits may not be very random. See:

http://www.eskimo.com/~scs/c-faq.com...tveryrand.html
http://www.eskimo.com/~scs/c-faq.com/lib/randrange.html

A third possible problem is that it makes the numbers (very) slightly
biased, unless the maximum number that rand() returns is a multiple of
4000. Of course, for many practical purposes, this may not be
important, and if it is, a better generator should be used anyway.
Mar 29 '06 #21
In comp.lang.c, you wrote:
Anders Arnholm <An*********@Ar nholm.nu> wrote:
eksamor <ek*****@yahoo. com> skriver:
> I can't seem to control the rand() function. I just makes an evil
> humongeous number. How do I make it generate random numbers from 1 -
> 4000??

As stated befor you need to use srand() before using rand(), however
I would recoment to look into your operating system manual. Hopefully
you have someting better than the rand() interface for random numbers.
(However doing so takes you outside standard C.) I recomend
arc4random() if your system supports it.

What on earth is that? If it can be written in ISO C, perhaps that's a
better idea. Several pretty decent PRNGs can be written in ISO C; see,
for example, <S%************ ******@news1.rd c1.fl.home.com> . Or if you
want the sledgehammer approach, do a websearch on the Mersenne Twister.

A good random function can unfortunalty not yet be written i ISO C.
Without getting into system dependent things you can't get a good
entropy data from any place. If you can precent a good way to get
strong entropy data within ISO C, I stand corrected, but I don't
know of any interface to such stuff without knowing about the OS.
arc4random(), entropy data from any place. If you can precent a good way
to get strong entropy data within ISO C, I stand corrected, but I don't
know of any interface to such stuff without knowing about the OS.
arc4random(), is a good interface for this, rand() isn't.

However as this goes outside ISO C it has to be considerd for
portability. Desinging the same bad stuff over and over again
to keep inside ISO C have no place in good programing. As little
and using platform specific non ISO C stuff on things that ISO C
is as good at.

/ Anders
--
http://anders.arnholm.nu/ Keep on Balping
Mar 29 '06 #22
If you are using a DOS based compiler use the function randomize().
This will reinitialise the random number generator. So that you will
not have the same set of random numbers every time.

Thanks

Mar 30 '06 #23
In article <11************ *********@j33g2 000cwa.googlegr oups.com>, "Purush"
<pu*********@gm ail.com> wrote:
If you are using a DOS based compiler use the function randomize().
This will reinitialise the random number generator. So that you will
not have the same set of random numbers every time.

On my system I initialise the random number generator with the system time
this gives me a satisfactory new start on each run.

--
_ _______________ _______________ ___________
/ \._._ |_ _ _ /' Orpheus Internet Services
\_/| |_)| |(/_|_|_> / 'Internet for Everyone'
_______ | ___________./ http://www.orpheusinternet.co.uk
Mar 30 '06 #24
Purush wrote:

If you are using a DOS based compiler use the function randomize().
This will reinitialise the random number generator. So that you
will not have the same set of random numbers every time.

Please don't give such misinformation. There is no such standard C
function as randomize. However, srand() does exist. Use it when
you don't want the generator to always produce the same sequence.

For the means to do this on the broken google interface to usenet,
see below.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the

Mar 30 '06 #25
Anders Arnholm <An*********@Ar nholm.nu> wrote:
In comp.lang.c, you wrote:
Anders Arnholm <An*********@Ar nholm.nu> wrote:
As stated befor you need to use srand() before using rand(), however
I would recoment to look into your operating system manual. Hopefully
you have someting better than the rand() interface for random numbers.
(However doing so takes you outside standard C.) I recomend
arc4random() if your system supports it.
What on earth is that? If it can be written in ISO C, perhaps that's a
better idea. Several pretty decent PRNGs can be written in ISO C; see,
for example, <S%************ ******@news1.rd c1.fl.home.com> . Or if you
want the sledgehammer approach, do a websearch on the Mersenne Twister.

A good random function can unfortunalty not yet be written i ISO C.

Which is why I wrote PRNGs instead. For most applications, a good PRNG
is good enough, and those of us who do need a _real_ RNG know it.
arc4random(), entropy data from any place.

Impossible to implement reliably on many systems. Keyboard timing? You
get into trouble with CLI plumbing. Network timing? Not all machines are
networked. Brownian motion? Needs a TTC interface, and those are rare.

Richard
Mar 31 '06 #26
"Purush" <pu*********@gm ail.com> wrote:

[ Learn to quote! I'm led to believe that there is a button on Google
Broken Beta somewhere which will let you do so. ]
If you are using a DOS based compiler use the function randomize().

And what guarantee do you have that all DOS (_whose_ DOS, anyway?) based
compilers have this function? And that it works correctly with rand() in
all those compilers?

Richard
Mar 31 '06 #27
Richard Bos wrote
(in article <44************ ****@news.xs4al l.nl>):
Impossible to implement reliably on many systems. Keyboard timing? You
get into trouble with CLI plumbing. Network timing? Not all machines are
networked. Brownian motion? Needs a TTC interface, and those are rare.

Of course, on some current operating systems, the arrival time
of unsolicited spyware and virii could be used as an entropy
source, unless you can't sample them fast enough.

--
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Mar 31 '06 #28

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