473,382 Members | 1,647 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,382 software developers and data experts.

random number in [0, 99]

Hi, I am using the int version of rand() with MS Visual Studio. I would
like to generate random number in the range of [0, 99]. Right now I am
using

rand() * 100 / (RAN_MAX + 1)

I found that I got too much 0. 0 appears more than any other numbers.
How can I fix it?

Thanks,

qq

Feb 8 '06 #1
4 6208
qu******@yahoo.com writes:
Hi, I am using the int version of rand() with MS Visual Studio. I would
like to generate random number in the range of [0, 99]. Right now I am
using

rand() * 100 / (RAN_MAX + 1)

I found that I got too much 0. 0 appears more than any other numbers.
How can I fix it?


See question 13.16 in the C FAQ, <http://www.c-faq.com/>.

--
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.
Feb 8 '06 #2
In article <11**********************@g47g2000cwa.googlegroups .com>,
qu******@yahoo.com wrote:
Hi, I am using the int version of rand() with MS Visual Studio. I would
like to generate random number in the range of [0, 99]. Right now I am
using

rand() * 100 / (RAN_MAX + 1)

I found that I got too much 0. 0 appears more than any other numbers.
How can I fix it?


If RAND_MAX > INT_MAX / 100, that would have to be expected and would be
entirely your fault. What would your code do if RAND_MAX == INT_MAX? You
might try

(int) (rand () * (100.0 / (RAND_MAX + 1.0)))

instead.
Feb 8 '06 #3
qu******@yahoo.com wrote:
Hi, I am using the int version of rand() with MS Visual Studio. I would
like to generate random number in the range of [0, 99]. Right now I am
using

rand() * 100 / (RAN_MAX + 1)

^^ missing decimal point
1 is an integer,
RAN_MAX + 1 is an integer (but
may overflow)

rand() * 100 / (RAN_MAX + 1.0)
1.0 is floating point,
RAN_MAX + 1.0 is floating point
Feb 8 '06 #4
qu******@yahoo.com wrote:
Hi, I am using the int version of rand() with MS Visual Studio. I would
like to generate random number in the range of [0, 99]. Right now I am
using

rand() * 100 / (RAN_MAX + 1)

I found that I got too much 0. 0 appears more than any other numbers.
How can I fix it?

Thanks,

qq

The C-FAQ suggests something like..

rand() / (RAND_MAX / len + 1)

...where len in your case would be 100.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Feb 8 '06 #5

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

Similar topics

10
by: Virus | last post by:
Ok well what I am trying to do is have 1.) the background color to change randomly with 5 different colors.(change on page load) 2,) 10 different quotes randomly fadeing in and out in random...
4
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
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...
10
by: Johnny Snead | last post by:
Hey guys, Need help with this random sort algorithm private void cmdQuestion_Click(object sender, System.EventArgs e) { Random rnd = new Random(); //initialize rnd to new random object...
5
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...
8
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new...
4
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...
13
by: Peter Oliphant | last post by:
I would like to be able to create a random number generator that produces evenly distributed random numbers up to given number. For example, I would like to pick a random number less than 100000,...
8
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;...
2
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
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$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...
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...

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.