Jump to content

So I made a function that generates a random number and the return value is the random number. I want two variables in my program to be equal to random numbers. Basically it's like this:

 

randomNumber1 = randomNumberFunction (variable1, variable2)

randomNumber2 = randomNumberFunction (variable1, variable 2)

 

When I run the program it produces a random number but it produces the same random number for both randomNumber1 and randomNumber2. I want it to produce two different random numbers. The randomNumberFunction just returns the random number calculation value. Help?

 

P.S. Variable 1 and 2 are the min and max boundaries for the random number generator.

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
https://linustechtips.com/topic/584488-help-with-c-functions/
Share on other sites

Link to post
Share on other sites

So instead of seeding my rand function in the function I seeded it in main, and now it produces two random numbers. Can anyone explain the reasoning behind this?

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
https://linustechtips.com/topic/584488-help-with-c-functions/#findComment-7623163
Share on other sites

Link to post
Share on other sites

its the way random works, its not actually random, its semi-random

it uses seeds

http://www.cplusplus.com/forum/beginner/5131/

http://stackoverflow.com/questions/9459035/c-rand-gives-same-number-when-running-process

 

btw its pretty much impossible to make a computer produce an actually random number

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/584488-help-with-c-functions/#findComment-7623181
Share on other sites

Link to post
Share on other sites

1 minute ago, Enderman said:

its the way random works, its not actually random, its semi-random

it uses seeds

http://www.cplusplus.com/forum/beginner/5131/

 

btw its pretty much impossible to make a computer produce an actually random number

That's not what I'm referring to but I know what you're talking about. I'm seeding rand with the time function (number of seconds in unix time since like 1970) which will produce a random number, every time, even on restarts of the program. If you seed it with the time function it's constantly fluctuating so it's not going to produce the same random number upon multiple restarts for example. Anyways getting a truly random number from the function wasn't the problem, it was where it was seeded I guess. If I seed in main it works fine, but if I seed in the function it produces the same random result. Hopefully you get what I'm saying I'm bad at explaining stuff.

 

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to comment
https://linustechtips.com/topic/584488-help-with-c-functions/#findComment-7623201
Share on other sites

Link to post
Share on other sites

11 minutes ago, Spev said:

That's not what I'm referring to but I know what you're talking about. I'm seeding rand with the time function (number of seconds in unix time since like 1970) which will produce a random number, every time, even on restarts of the program. If you seed it with the time function it's constantly fluctuating so it's not going to produce the same random number upon multiple restarts for example. Anyways getting a truly random number from the function wasn't the problem, it was where it was seeded I guess. If I seed in main it works fine, but if I seed in the function it produces the same random result. Hopefully you get what I'm saying I'm bad at explaining stuff.

 

Yeah it might be because its getting called at pretty much the same time and therefore the seed is identical?

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/584488-help-with-c-functions/#findComment-7623248
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

×