produce "random" numbers
Hello I just started programming about a month or so ago. I am doing a program that will produce "random" numbers. I am confused on how I need to do this. I have written some code that will follow(some of the variables declared aren't being used I was just experimenting). Any help to guide me in the right direction is appreciated. The problem states: Using the power residue method,
a) Create a function randnum() that accepts a floating point "seed" as a parameter and returns a floatingpoint random number between 0 and 1.e6. b) Incorporate the randnum() function created in 7a into a working program that produces 10 random numbers between 0 and 1.e6. Code:

Re: produce "random" numbers
The way pseudorandom numbers generally work is to modify the seed according to some formula then use the new value as the next seed. So:
initialise seed < this is the number you enter random number = f(seed) < this function returns a random number as a function of seed random number 2 = f(random number) < so the result of the previous is fed into the function again. So you need to keep seed somewhere instead of discarding it, so I would suggest making the "please enter" part a separate part of randnum[ber]() and pass the seed into this function. A for loop will do the "display 10 random numbers" bit. 
Re: produce "random" numbers
Quote:
Code:
#include<iostream> 
Re: produce "random" numbers
> even if it is not divisible it still produces the results
I thought it was supposed to produce results if it is not divisible. It's if it IS divisible that the number isn't valid. What number did you enter? After cin>>x you could try displaying the number entered as a check that x contains what you're expecting. Why is your loop variable i a double? 
Re: produce "random" numbers
Quote:
O yes you are correct. It's one of those things where you know what you meant but typed it wrong. i should be int, I don't know why I had double. An example number I entered would be 346555. If it IS divisible then I want the program to tell the user the number is not valid and end. If it is NOT divisible then I want the program to continue and loop out the ten random numbers. 
Re: produce "random" numbers
Also, I tried numbers such as: 9000 900000 500000 345000..All of these numbers will produce an error. If I type 345555 this number will pass through and produce the random numbers, even though it is divisible by 5. I'm a little confused as to why this is happening.

Re: produce "random" numbers
Don't know why 345555 passes through, it is certainly divisible by 5. Compiler bug perhaps? What compiler are you using?
Did you try printing the number entered back on screen to check the variable contains the expected value? Paste the following function into your code, call it, and let me know what the output was: Code:
void go4e_43398() 
Re: produce "random" numbers
Quote:
15 is divisible by 2 or 5 20 is divisible by 2 or 5 345555 is divisible by 2 or 5 (hmmmmmm why is it working here?) 36 is divisible by 2 or 5 40 is divisible by 2 or 5 size of int =4 
Re: produce "random" numbers
Quote:
(x%5 == 0 && x%2 == 0) Obviously 345555 is divisible by 5 but is not divisible by 2, 9000 900000 and all of those numbers are divisible by both. That is why the number was not passing through. I changed it to if (x%5 == 0  x%2 == 0) now it can be either or. 
Re: produce "random" numbers
That's funny, because in your "Thanks. I have another question." post you actually posted "if (x%5 == 0  x%2 == 0)".

All times are GMT +5.5. The time now is 02:59. 