Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 86,371 C++ Programmers. There are 1,457 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

randomized number...

 
Reply to this topicStart new topic

randomized number..., -1 till 1

zattara13
post 8 May, 2008 - 10:46 PM
Post #1


New D.I.C Head

*
Joined: 27 Mar, 2008
Posts: 23



hi i am trying to generate a random number between -1 and 1. all i mamanged to do it to generate 2 random float numbers - from -1 till 0, and from 0 till 1. any help how i can generate just one number instead of two? 10x

CODE


printf("%f\n\n", ((rand()) / ( float ) (RAND_MAX + 1));

printf("%f\n\n", ((rand()) / ( float ) (-RAND_MAX + 1));

User is offlineProfile CardPM
Go to the top of the page
+Quote Post


no2pencil
post 8 May, 2008 - 11:12 PM
Post #2


DIC K-mart

Group Icon
Joined: 10 May, 2007
Posts: 3,322

I use the following for generating random.

cpp

int getrand(int min,int max){
return(rand()%(max-min)+min);
}


So you could simply throw this function 0,1 like so:

cpp

int result=getrand(0,1);
printf("%i\n",result);
User is online!Profile CardPM
Go to the top of the page
+Quote Post

zattara13
post 8 May, 2008 - 11:19 PM
Post #3


New D.I.C Head

*
Joined: 27 Mar, 2008
Posts: 23

I fully agree with you, but my problem is not generating from 0 to 1, but from -1 to 1!! the comand you gave me is between 0 and 1 and i've done that a bunch of times, but i was never asked to randomize between -1 and 1, both included. 10x smile.gif

QUOTE(no2pencil @ 8 May, 2008 - 11:12 PM) *

I use the following for generating random.

cpp

int getrand(int min,int max){
return(rand()%(max-min)+min);
}


So you could simply throw this function 0,1 like so:

cpp

int result=getrand(0,1);
printf("%i\n",result);


User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Cerolobo
post 8 May, 2008 - 11:27 PM
Post #4


D.I.C Head

**
Joined: 5 Apr, 2008
Posts: 151

no2pencil's code will work for -1 to 1, but it will only generate a integer

CODE

float num = 2.0f / (rand() + 1) - 1.0f;


or function form

CODE

float RandFloat(float min, float max)
{
  return (max - min) / (rand() + 1) + min;
}


So here, if rand generates a 0, we will have

2.0f / (0 + 1) - 1.0f;

so, we get

2.0f / 1 - 1.0f = 2.0f - 1.0f = 1.0f

if rand() generates a huge number, we get

2.0f / (huge number + 1) - 1.0f = <almost 0> - 1.0f = -1.0f

So, the bigger the number rand() generates, the closer it will be to -1.0f, and the smaller, the closer to 1.0f the number will be.

Edit: Added the function form.

This post has been edited by Cerolobo: 8 May, 2008 - 11:31 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

no2pencil
post 8 May, 2008 - 11:44 PM
Post #5


DIC K-mart

Group Icon
Joined: 10 May, 2007
Posts: 3,322

QUOTE(zattara13 @ 9 May, 2008 - 02:19 AM) *

I fully agree with you, but my problem is not generating from 0 to 1, but from -1 to 1!!

Oops, I misread. Sorry.
User is online!Profile CardPM
Go to the top of the page
+Quote Post

Cerolobo
post 8 May, 2008 - 11:45 PM
Post #6


D.I.C Head

**
Joined: 5 Apr, 2008
Posts: 151

Sorry, I just ran the math, and the above formula generates sub optimal results. Here is a much better version

CODE
float RandFloat(float min, float max)
{
  return ((float)rand() / RAND_MAX) * (max - min) + min;
}


(float)rand() / RAND_MAX
will generate a number between 0 and 1.

* (max - min)
Will scale by the range of the numbers.

+ min;
will offset the number to the starting position.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
Time is now: 5/17/08 02:36AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month