Random number between 1 and 4000?

E

eksamor

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??
 
C

Chris Dollin

eksamor said:
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??

You can't. But you can do something with the gynormous value you've
got.
 
E

eksamor

I now made this:

int blop;

blop = rand();
while (blop > 4000 || blop < 1)
{
blop = rand();
}
printf("blop: %d\n",blop);

but it always prints "blop: 3722"
 
R

Ralph A. Moritz

eksamor said:
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??

You need to first seed rand(), using srand(). Below is a simplistic
example.

Regards,
Ralph


#include <stdio.h>
#include <stdlib.h>

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

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

Anders Arnholm

eksamor said:
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.

/ Anders
 
M

Martin Ambuhl

eksamor said:
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??

Beginners often seem to believe that they have discovered new problems,
but that is rarely so. In fact, most problems they run into have been
encountered many rimes in the past. This is the reason that many
newsgroups have "Frequently Asked Question" lists (FAQs). It is
normally expected that a poster will have checked the FAQ _before_
posting. Sometimes people will complain that they had no idea that
there was a FAQ or where it might be. But it is also expected behavior
to follow a newsgroup before posting. In almost every newsgroup with a
FAQ, the FAQ itself or at least a document pointing to it will be posted
about twice a month. Another objection often seen is that new posters
don't know about these simple guidelines of looking for the FAQ or
following the newsgroup. This is similar to claiming that there is no
need to check which side of the road people drive on when one goes to
another country. Unfortunately, for reasons known only to themselves,
people who provide newsreaders no longer provide them with subscriptions
already built in to < You need to do that
yourself.

In the case of <the FAQ can be found at
<http://c-faq.com/>. In your case, the particular question you need to
check is
<http://c-faq.com/lib/randrange.html>
"Question 13.16: Q: How can I get random integers in a certain range?".
 
F

Flash Gordon

eksamor said:
that looks nice but it always prints 1384

Can *everyone* please provide context when replying. Google is *not*
Usenet and there is no guarantee that the post you are replying to has
been seen by other people. See
http://www.safalra.com/special/googlegroupsreply/ and
http://cfaj.freeshell.org/google/ for details.

Also see the link in my sig for more information about this group.

eksamor, rather than making people guess post a small *complete* program
showing your problem, copy and paste, do *not* retype. Since you
obviously don't know what the problem is you have no way of knowing
which line you haven't posted is the cause of the problem.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

eksamor said:
that looks nice but it always prints 1384
You're either using/printing it wrongly,
or you are calling rand() once per invocation of your program,
and havn't set a seed (the srand function) that varies.
rand() yields pseudo random numbers, it will generate the same
sequence every time for a given seed.
 
N

Nick Keighley

eksamor wrote:

1. include some context in your post (quote who you are replying to)
I now made this:

int blop;

blop = rand();
while (blop > 4000 || blop < 1)
{
blop = rand();
}

this may not terminate- and it is certainly ineffecient
printf("blop: %d\n",blop);

but it always prints "blop: 3722"

read up a pseudo-random number generators. In short they always
produce the same sequence unless you seed them.


--
Nick Keighley

Many astrologers think that this concentration on
[the sun-sign column] has done untold damage to serious astrology.
The Independent
 
N

Nick Keighley

Doc said:
u can use like this:

rand()%4000 + 1

1. put some context in your posts
2. its "you" not "u"
3. read the fact before you post rubbish like this
 
A

A. Sinan Unur

You need to first seed rand(), using srand(). Below is a simplistic
example.
....

#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/lib/notveryrand.html

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

Sinan
 
R

Richard Bos

Anders Arnholm said:
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%[email protected]>. Or if you
want the sledgehammer approach, do a websearch on the Mersenne Twister.

Richard
 
J

Jordan Abel

read up a pseudo-random number generators. In short they always
produce the same sequence unless you seed them.

You can't "not seed" them. The issue is, rather, that the C standard
requires that it is always seeded with the same value at program start,
and if you want different sequences you need to re-seed it.
 
A

Arndt Jonasson

A. Sinan Unur said:
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/lib/notveryrand.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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,947
Members
47,501
Latest member
Ledmyplace

Latest Threads

Top