473,902 Members | 5,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

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.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
"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/googlegroupsrep ly/>

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.

--
Randy Howard (2reply remove FOOBAR)
"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.

Similar topics

10
2917
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 have is that the first loop I get a sequence of random numbers untuil I get a match, BUT then on the following loops I get the SAME random(?) sequence. I am using rand(). I do not want to get too fancy with the random number generator, but is there...
4
2705
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;
70
6329
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 using the high-order bits returned by rand() instead of the low-order bits, avoiding using rand() for anything that wants decently random numbers, not using rand() if you want more than approx. UINT_MAX total different sequences, and so on. So I...
16
12088
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 numbers using asp.net? I know about the random namespace within .net, but I need a reference to some code that do the similar stated function above. Plus If you have any coding practice ideas for the above defined project please share them.
5
3359
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 double value in the range could come up with equal probability). I'd also like to be able to seed this generator (e.g., via the clock) so that the same sequence of random values don't come up every time. Anybody have an easy and fast...
2
1988
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. It only stays at the first images link. Is this a cache issue? Or is there anyway to create a random number to trick this or make it work properly. I'm very raw with Javascript so I'm having trouble figuring this out. Thank you in advance
4
10611
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 guessed right, it asks the user is he/she wants to play again. If the answer is yes, it generates a random number and asks the user to guess the number again. The user can exit if he enters 0. I have created the following code so far but it does not work....
8
7568
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; i<count; i++) { Random Rnd = new Random(); number = number+Convert.ToString(Rnd.Next(0,9));
2
5487
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 - At one point, I need a random number (float) in the range 0.5 to 1.5 - At one point, I need a random number (float) in the range 0.3 to 3.0 - At one point, I need a random number (float) in the range 0.1 to 10.0 Also, I need to make it sure...
0
9997
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9845
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11279
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10872
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10499
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9675
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7205
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4307
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.