Jump to content

random numbers

JewishBacon

so im working on a little homework assignment i have. since my prof is a student, and only mumbles on the rare occasion when he speaks coherent English, i basically have to learn everything by myself.

the idea is to make a random number between [0,1] and take the arctan of that number, I got the whole code working, sans the random number. i want a float number, but it only gives me integers. what am i missing? 

 

here is my code so far:

 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main()
{
        float  i,n, data;
 
                for (i = 1; i <= 10; i++)
                {
                n = rand()%1;
                data = atan(n);
                printf("random# is =  %f     ,  atan(x)= %f \n", n, data);
                }
 
  return 0;
}
 
thanks in advanced

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

rand() gives a number between 0 and RAND_MAX

To turn it into a float between 0 and 1 you just divide by the maximum value. Make sure you cast one of the numbers to a float first so it does a floating point division.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

rand() gives a number between 0 and RAND_MAX

To turn it into a float between 0 and 1 you just divide by the maximum value. Make sure you cast one of the numbers to a float first so it does a floating point division.

i think i did somehing like that 

 

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
 
int main()
{
        float  sum=0,i,n,d, data, avg;
                int iter =100000;
                for (i = 1; i <= iter; i++)
                {
                n = rand()%100+1;
                d= 1/n;
                data = atan(d);
        /*      printf("d= %f random# is =  %f  ,  atan(x)= %f \n",d, n, data);  */
                sum = sum +data;
                }
        avg = sum/iter;
        printf("sum = %f   ,   avg = %f,  \n", sum, avg);
return 0;
}

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

Did you try googling "Random float between 0 and 1 C++"? :P Most of learning programming is just googling it and getting good at it.

 

Stack overflow has a good explanation:

 

http://stackoverflow.com/questions/686353/c-random-float-number-generation

Workstation: i7-4930k | Asus Rampage IV Gene | Reference GTX 780 | 32GB Crucial Ballistix | 500GB Samsung 840 EVO | Corsair RM650 | MidNight Black BitFenix Prodigy M

 

Old Rigi5-2500k @ 4.7 GHz | Asus P8P67 Deluxe | EVGA GTX 560 | 16GB Corsair Vengeance | 240 GB Samsung 830 Pro | 1TB Hitachi | CoolerMaster Storm Scout (1)

Link to comment
Share on other sites

Link to post
Share on other sites

rand() gives a number between 0 and RAND_MAX

To turn it into a float between 0 and 1 you just divide by the maximum value. Make sure you cast one of the numbers to a float first so it does a floating point division.

um i dont suppose you can tell me why im not gettign a value for sum of .7?

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

Did you try googling "Random float between 0 and 1 C++"? :P Most of learning programming is just googling it and getting good at it.

 

Stack overflow has a good explanation:

 

http://stackoverflow.com/questions/686353/c-random-float-number-generation

is there a way i can get the random numbers to not repeat?

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

is there a way i can get the random numbers to not repeat?

You need to seed the RNG

srand(time(NULL))

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

 

You need to seed the RNG

srand(time(NULL))

im confused. i just add that line to my code and nothing will repeat?

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

im confused. i just add that line to my code and nothing will repeat?

Do you mean not repeat any 1 number? Or not repeat every time you run the program?

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

is there a way i can get the random numbers to not repeat?

 

Oh yeah, sorry,  as fizzlesticks said, you have to change the seed for it to change. This is so you can generate "consistent" randomness, if that makes since (so it's the same during subsequent runs of the program if you want). You need to add that time at the beginning if I remember correctly. It basically sets the seed to the time at the beginning of your program, thus making it different for every run.

Workstation: i7-4930k | Asus Rampage IV Gene | Reference GTX 780 | 32GB Crucial Ballistix | 500GB Samsung 840 EVO | Corsair RM650 | MidNight Black BitFenix Prodigy M

 

Old Rigi5-2500k @ 4.7 GHz | Asus P8P67 Deluxe | EVGA GTX 560 | 16GB Corsair Vengeance | 240 GB Samsung 830 Pro | 1TB Hitachi | CoolerMaster Storm Scout (1)

Link to comment
Share on other sites

Link to post
Share on other sites

Do you mean not repeat any 1 number? Or not repeat every time you run the program?

 

 

Oh yeah, sorry,  as fizzlesticks said, you have to change the seed for it to change. This is so you can generate "consistent" randomness, if that makes since (so it's the same during subsequent runs of the program if you want). You need to add that time at the beginning if I remember correctly. It basically sets the seed to the time at the beginning of your program, thus making it different for every run.

 

i need it to never repeat a number during any of the iterations.  so if i just put srand(time(NULL)) it  should work? do i just put it at the beginning where i call out my floats? or does it go in the for loop?

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

i need it to never repeat a number during any of the iterations.  so if i just put srand(time(NULL)) it  should work? do i just put it at the beginning where i call out my floats? or does it go in the for loop?

Ok, that's a bit more complicated.  I can't really tell from the code, are you using C++ or straight C?

 

But you should put srand(time(NULL)) at some point before the loop anyway so that each time you run the program you'll get a different result.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, that's a bit more complicated.  I can't really tell from the code, are you using C++ or straight C?

 

But you should put srand(time(NULL)) at some point before the loop anyway so that each time you run the program you'll get a different result.

im working in c

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

im working in c

OK, there are 2 ways to go about doing it. The basic idea is to store each number you generate in a list and each time you get a new one check if the number already exists in that list.

 

1) The easier way would be to make an array the same size as the amount of numbers you need.

2) Keep a pointer to the current position in the array and each time you get a new valid number put it in the array at that position and increment the pointer.

3) Inside the for loop, use a do while loop to choose the next random number.

int randomNumberdo{    randomNumber = rand(); //with any mod or addition you want} while(randomNumber in list of numbers);listOfNumbers[currentPosition] = randomNumber;currentPosition++;

4) Take that new random number and do whatever you want with it.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

please note that

                n = rand()%100+1;                d= 1/n;

only generates 100 different random numbers, and because of the behaviour of 1/n they are, for example, more likely to be very very small numbers rather than 0.5 or something like that

the atan will make them collapse even closer to 0

 

as someone suggested before, the best way to get a number 0 < n < 1 using rand() is to divide it by the maximum number that rand() can generate

(float) rand() / RAND_MAX

if you want to check some of the gossip behind random numbers generation, there is an entertaining video for you

http://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

Link to comment
Share on other sites

Link to post
Share on other sites

please note that

                n = rand()%100+1;                d= 1/n;

only generates 100 different random numbers, and because of the behaviour of 1/n they are, for example, more likely to be very very small numbers rather than 0.5 or something like that

the atan will make them collapse even closer to 0

 

 

thats ok i need them to be small. ive changed the code since then to be 

n = rand()%10000+1;

d= 1/n;

STEAM NAME: JewishBacon GPU  Sapphire dual x R9 280x OC edition CPU core i7 4770k stock speed COOLER H100i  CASE Fractal R4 Window Black  MOBO MSI gd-65 gaming Storage 1TB WD Blue drive, 1TB Samsung 7200 rpm, 120 GB OCZ SSD, 64 GB WD Blue ssd  RAM 12 GB @ 1600 Ghz kingston RAM  MiscNZXT HUE, disk read/write, 2x 21 inch 1920x1080 monitors   

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×