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?? 27 9756
eksamor wrote: 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.
--
Chris "x.f == f(x)" Dollin
Beware the ideas of March.
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"
u can use like this:
rand()%4000 + 1
first,srand((unsigned)time(NULL)),
then,rand()
that looks nice but it always prints 1384
eksamor wrote: 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;
}
--
Ralph Moritz
Laugh at your problems; everybody else does.
"eksamor" <ek*****@yahoo.com> writes: 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??
The comp.lang.c FAQ is at <http://www.c-faq.com/>. Read questions
13.15, 13.16, and 13.17.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
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.
/ Anders
-- http://anders.arnholm.nu/ Keep on Balping
eksamor wrote: 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 <news:news.announce.newusers>. You need to do that
yourself.
In the case of <news:comp.lang.c>, 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?".
eksamor wrote: 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.
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
Doc wrote: 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
--
Nick Keighley
eksamor wrote: 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.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro: http://clc-wiki.net/wiki/Intro_to_clc
"Ralph A. Moritz" <ra*******@gmail.com> wrote in
news:11**********************@u72g2000cwu.googlegr oups.com: eksamor wrote: 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.
....
#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
Sinan
--
A. Sinan Unur <1u**@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
Anders Arnholm <An*********@Arnholm.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.rdc1.fl.home.com>. Or if you
want the sledgehammer approach, do a websearch on the Mersenne Twister.
Richard
On 2006-03-28, Nick Keighley <ni******************@hotmail.com> wrote: 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.
"John Bode" <jo*******@my-deja.com> writes: eksamor wrote: 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??
Follow the link: http://www.eskimo.com/~scs/c-faq.com/lib/randrange.html
That link works, but the FAQ is now in its own domain. See
<http://www.c-faq.com/lib/randrange.html>.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Martin Ambuhl wrote: eksamor wrote:
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.
That's a poor analogy.
Most severe consequence of posting to a newsgroup without knowing the
simple guidelines: mental anguish
Most severe consequence of driving on the wrong side of the road in a
foreign country: death
--
jay
"A. Sinan Unur" <1u**@llenroc.ude.invalid> writes: "Ralph A. Moritz" <ra*******@gmail.com> wrote in news:11**********************@u72g2000cwu.googlegr oups.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.
In comp.lang.c, you wrote: Anders Arnholm <An*********@Arnholm.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.rdc1.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
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
In article <11*********************@j33g2000cwa.googlegroups. com>, "Purush"
<pu*********@gmail.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
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.
In addition, ensure you include adequate context for your replies.
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.com, 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
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
Also see <http://www.safalra.com/special/googlegroupsreply/>
Anders Arnholm <An*********@Arnholm.nu> wrote: In comp.lang.c, you wrote: Anders Arnholm <An*********@Arnholm.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.rdc1.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
"Purush" <pu*********@gmail.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
Richard Bos wrote
(in article <44****************@news.xs4all.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.
--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Sonoman |
last post by:
Hi all:
I am trying to write a simple program that simulates asking several persons
their birth day and it counts how many persons are asked until two have the
same birth day. The problem that I...
|
by: Jack |
last post by:
I have two files:
sort_comparison.c++
my_sort.h
sort_comparison.c++ calls this code in my_sort.h:
void my_sort::fillArray(int arr,int n)
{
// const int random_number_range=1000000;
|
by: Ben Pfaff |
last post by:
One issue that comes up fairly often around here is the poor
quality of the pseudo-random number generators supplied with many
C implementations. As a result, we have to recommend things like...
|
by: Leon |
last post by:
I need a program that generate 5 non-duplicates random number between 1-10
as string values store in an array.
Do anybody know of any good books or websites that explain how to generator
random...
|
by: Peteroid |
last post by:
I know how to use rand() to generate random POSITIVE-INTEGER numbers.
But, I'd like to generate a random DOUBLE number in the range of 0.0 to 1.0
with resolution of a double (i.e., every possible...
|
by: mikeoley |
last post by:
Ok I have a Javascript slideshow working. Every image is linked to a
another page in the site. Problem is...The link wont refresh to the
next link once the second images rollovers in the slideshow....
|
by: fatimahtaher |
last post by:
Hi,
I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
|
by: Anil Gupte |
last post by:
I had someone write a random number generator in C# (I am more of a VB
programmer) and they came up with the following:
public string GetRand(int count)
{
string number = "";
for (int i=0;...
|
by: alishaikhji |
last post by:
I am working on a program which will need several different integer and float random numbers at different stages, for example:
- At one point, I need a random number (float) in the range 0.1 to 10.0...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
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,...
|
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,...
|
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...
| |