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

Different seeding of srand() during a SINGLE program run

Hi,
I am using srand() and would like to create different random numbers
during a SINGLE execution of my program because I want to compare
random cases.
For now I have a switch statement within a loop, which does not do the
job. I get the same output from my simulator every time. Does it mean
that it actually can seed ONCE per program run?
Thanks
for(i=....; i++){
switch (i){
case 1: seed = 10; break;
case 2: seed = 20; break;
default: printf("error"); break
}
srand(seed);

//code that uses Random()
}

Nov 15 '05 #1
3 2159
On 2 Oct 2005 20:52:31 -0700, "bobrics" <bo*****@gmail.com> wrote in
comp.lang.c:
Hi,
I am using srand() and would like to create different random numbers
during a SINGLE execution of my program because I want to compare
random cases.
For now I have a switch statement within a loop, which does not do the
job. I get the same output from my simulator every time. Does it mean
that it actually can seed ONCE per program run?
Thanks
No, you can use srand() to seed rand() as many times as you like in a
program.
for(i=....; i++){
switch (i){
case 1: seed = 10; break;
case 2: seed = 20; break;
default: printf("error"); break
}
srand(seed);

//code that uses Random()
}


What is Random()? It is not a standard C function. It does whatever
its documentation says it does, and we have no idea whether it uses
the seed set by srand() or not.

How does your reseeding of srand() work when you use rand() to
retrieve values?

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #2
"bobrics" <bo*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Hi,
I am using srand() and would like to create different random numbers
during a SINGLE execution of my program because I want to compare
random cases.
For now I have a switch statement within a loop, which does not do the
job. I get the same output from my simulator every time. Does it mean
that it actually can seed ONCE per program run?


The pseudo-random number generator that is implemented in most of the
standard libraries is purely deterministic. If you want different sequences
of numbers from a call to call, you must initialize the seed differently
every time. If from a run to run the inital seed is the same, the sequences
will be the same too. Perhaps, all you need is to initialize with something
like current time (since e.g. epoch or computer power up) in seconds, e.g.
use time(NULL) return value...

Alex
Nov 15 '05 #3
thank you guys for all the replies.

I have made a mistake in some other place, which caused the code under
test to execute once, even though it was run several times. So, it
created an illusion that my seeder didn't do the job properly. Well,
two lessons are learned here. First, is that srand() can generate
different numbers within the same run, as expected! : )
And second one is that when you take a code and think of it as a
blackbox and plan to run it several times with different parameters -
do not forget to reset ALL variables to initial values, and not only
the ones under test!

Leave and learn,
Thank you for all the replies again!

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Joe | last post by:
Hi, I have been working on some code that requires a high use of random numbers within. Mostly I either have to either: 1) flip a coin i.e. 0 or 1, or 2) generate a double between 0 and 1. I...
4
by: Jack | last post by:
I have two files: sort_comparison.c++ my_sort.h sort_comparison.c++ calls this code in my_sort.h: void my_sort::fillArray(int arr,int n) { // const int random_number_range=1000000;
7
by: A_StClaire_ | last post by:
hi, I'm working on a project spanning five .cpp files. each file was used to define a class. the first has my Main and an #include for each of the other files. problem is my third file...
25
by: Merrill & Michele | last post by:
Many of us watched the World Series of Poker this last week and plotted how we were to take over that world. My current problem begins with shuffling the deck. For the apps I've written before,...
17
by: romixnews | last post by:
Hi, I'm facing the problem of analyzing a memory allocation dynamic and object creation dynamics of a very big C++ application with a goal of optimizing its performance and eventually also...
3
by: sttip | last post by:
Hi, I'm a student taking a beginner's c++ course. I am trying to write a program that performes simple math operations on random numbers. I am using the srand(int(time(0))); function to generate...
9
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
11
by: Masud | last post by:
hi, for a test i want to generate different random numbers between different ranges in a single loop. I tried to solve in the following way but it always profile same value. I am looking for...
12
by: Lynn | last post by:
Excuse me. Is the sentence below a statement or a function; srand(time(NULL));
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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.