473,407 Members | 2,629 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,407 software developers and data experts.

What's wrong with my random number generation?

Here's the code:

signed char xMove, yMove, subx, suby;

[a bunch of stuff snipped]

xMove=(3*((float)rand()/(float)RAND_MAX))-1;
yMove=(3*((float)rand()/(float)RAND_MAX))-1;

I run srand() in main. When I run it without the -1 I get 0-2 as expected
but when I run it with them I get 0 and 1.

Why?

--
Mark Healey
marknews(at)healeyonline(dot)com

Apr 28 '06 #1
2 2142
In article <pa****************************@spammer.die>,
Mark Healey <di*@spammer.die> wrote:
Here's the code: signed char xMove, yMove, subx, suby; [a bunch of stuff snipped] xMove=(3*((float)rand()/(float)RAND_MAX))-1;
yMove=(3*((float)rand()/(float)RAND_MAX))-1; I run srand() in main. When I run it without the -1 I get 0-2 as expected
but when I run it with them I get 0 and 1. Why?


rand() will rarely come out as RAND_MAX, so the division is almost
always going to come out as something less than 1. When you multiply
it by 3, you are almost always going to come out with something less than 3.
Subtract 1 and you are almost always dealing with something less than 2.
You then convert that to an integer value by truncation instead of
rounding, so the maximum value will almost always be 1.

About 1/3 of the time, the float division of rand() by RAND_MAX will
come out less than 1/3; multiplied by 3 that would come out less than 1;
subtract 1 and the result will be less than 0. You then convert that
to a signed integer value. Check your results again: you are
probably generating some -1's.

Note by the way that the constants 3 and 1 in your expression
are operating on float values, so you might as well use float constants,
e.g. 3.0f and 1.0f .
--
Is there any thing whereof it may be said, See, this is new? It hath
been already of old time, which was before us. -- Ecclesiastes
Apr 28 '06 #2
ro******@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote:
In article <pa****************************@spammer.die>,
Mark Healey <di*@spammer.die> wrote:
Here's the code:
signed char xMove, yMove, subx, suby; xMove=(3*((float)rand()/(float)RAND_MAX))-1;
yMove=(3*((float)rand()/(float)RAND_MAX))-1;

I run srand() in main. When I run it without the -1 I get 0-2 as expected
but when I run it with them I get 0 and 1.

rand() will rarely come out as RAND_MAX, so the division is almost
always going to come out as something less than 1. When you multiply
it by 3, you are almost always going to come out with something less than 3.
Subtract 1 and you are almost always dealing with something less than 2.
You then convert that to an integer value by truncation instead of
rounding, so the maximum value will almost always be 1.


The solution to this problem is to use the right way to get a random
number within a range, as detailed in the FAQ:
<http://www.c-faq.com/lib/randrange.html>.

Richard
Apr 28 '06 #3

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...
10
by: Ioannis Vranos | last post by:
I want to create some random numbers for encryption purposes, and i wonder if the following scheme makes it more hard to guess the underneath number generation pattern, than the plain use of...
15
by: ben | last post by:
this programme from sedgewick's 'algorithms in c parts 1-4' "is a sample client program that ... uses a symbol table to find the distinct values in a sequence of keys (randomly generated or read...
5
by: user | last post by:
Hello i call it in almost same time (from two different threads): Random rand = new Random(); id=rand.Next(65535); and i often receive the same results. Is it possible ? How windows count this...
23
by: Alvin | last post by:
Well, I'm developing a Tetris game in SDL, but when it comes to deciding the next block, I'm stuck. It's random, but when I try something like seeding the randomizer with the time, it won't update...
6
by: Anamika | last post by:
I am doing a project where I want to generate random numbers for say n people.... These numbers should be unique for n people. Two people should not have same numbers.... Also the numbers...
7
by: alphaLaura | last post by:
Hi all - I'm just new to the group and I hope nobody minds me asking for some help. I currently have an assignment which deals with matrices (more specifically, Gauss-Seidel solving of...
21
by: chico_yallin | last post by:
I just wana make a random id number based on4 digits-for examples?? Thanks in Advance Ch.Yallin
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;...
16
by: jason.cipriani | last post by:
I am looking for a random number generator implementation with the following requirements: - Thread-safe, re-entrant. - Produces consistently reproducible sequences of psuedo-random numbers...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.