473,805 Members | 2,059 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

srand() troubles

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, I've always been satisfied with the usual:

#include <time.h>
#include <stdio.h>

int main( 2 things) {
time_t timer;
int tja;
srand(&timer);

tja=rand();
printf("%?", tja);
return(0);
}
The problem is that the time is always around 109 billion. This, in turn,
causes the generated pseudo-random to be around 23,000 with my
implementation. Even though I've been careful to slice up the 32768
outcomes of rand() in a reasoned way, the initial tilting has me wondering.
Two questions.
A) Do reasonable probabilists use rand(), given that you're not fooling
Gian Carlo Rota, but Chris Moneymaker?
B) If yes to A), then I think I'd want take that timer value, mask out the
things that don't change, maybe do a flip and an Xor, and then seed srand().
Does that sound like I'd then get values for the initial rand call with mu
around 16,000 and a bell curve?
Nov 14 '05 #1
25 2746
"Merrill & Michele" <be********@com cast.net> writes:
My current problem begins with shuffling the deck.


http://www.stanford.edu/~blp/writings/clc/shuffle.html
http://www.stanford.edu/~blp/writings/clc/random.html
--
"...Almost makes you wonder why Heisenberg didn't include postinc/dec operators
in the uncertainty principle. Which of course makes the above equivalent to
Schrodinger's pointer..."
--Anthony McDonald
Nov 14 '05 #2
"Merrill & Michele" <be********@com cast.net> writes:
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, I've always been satisfied with the usual:

#include <time.h>
#include <stdio.h>

int main( 2 things) {
time_t timer;
int tja;
srand(&timer);

tja=rand();
printf("%?", tja);
return(0);
}
Show us real code. We can't guess how your pseudo-code differs from
the code you actually compiled.
The problem is that the time is always around 109 billion. This, in turn,
causes the generated pseudo-random to be around 23,000 with my
implementation. Even though I've been careful to slice up the 32768
outcomes of rand() in a reasoned way, the initial tilting has me wondering.
Don't assume that there are 32768 possible values; use RAND_MAX.
Two questions.
A) Do reasonable probabilists use rand(), given that you're not fooling
Gian Carlo Rota, but Chris Moneymaker?
B) If yes to A), then I think I'd want take that timer value, mask out the
things that don't change, maybe do a flip and an Xor, and then seed srand().
Does that sound like I'd then get values for the initial rand call with mu
around 16,000 and a bell curve?


The standard guarantees a few things about the behavior of rand() and
srand(), but much of their behavior is implementation-defined. For
example, it's not uncommon for rand() to return alternating odd and
even numbers. Many implementations provide higher quality random
number generators.

There's also some good information in section 13 of the C FAQ,
<http://www.eskimo.com/~scs/C-faq/faq.html>.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #3
"Merrill & Michele" <be********@com cast.net> writes:
Show us real code. We can't guess how your pseudo-code differs from
the code you actually compiled.
Thanks both. The chance of me being able to reproduce code on a console
that does anything except draw scorn from a compiler is vanishingly small.
Furthermore, your output will be different from mine as a matter of
cosmology. I've attached a screen dump that shows the situation without
showing too much bad programming form. (Do people prefer to see code pasted
into the body of a message or as an attachment?)


Attachments are strongly discouraged, binary attachments even more so.
If you want to post a code sample, paste it into the body of your
message. If at all possible, post a small self-contained compilable
program. Take a look at some of the other articles in the newsgroup
to get an idea of how things are done around here.

If you're really unable to create compilable C code, I'm afraid you
may not be ready for us to help you. Learn the basics of the language
first. Kernighan & Ritchie's _The C Programming Language_, 2nd
edition, is widely considered to be one of the best tutorials. Work
through the exercises.

Going back to the pseudo-code you posted earlier, you had:

time_t timer;

srand(&timer);

srand() expects an unsigned int argument; you're passing it the
address of a time_t variable, which makes no sense. If your compiler
doesn't warn you about this, either it's a really bad compiler, or
you're missing a "#include <stdlib.h>" directive, or you've suppressed
or ignored diagnostic messages somehow.
I had no idea that I could have other than 2^15 outcomes. How often is this
the case, given that I'm not ever going to play Texas Hold'em with an
embedded system? MPJ


Your system should have documentation for the standard library
functions, include srand() and rand(). If you can't find that,
consult any decent C textbook or do a web search. And I already
pointed you to the C FAQ.

A quick summary:

srand() takes an unsigned int argument which is used as the seed for a
sequence of pseudo-random numbers. It should normally be called
exactly once in your program, before any calls to rand(). The call
srand(time(NULL ));
is often good enough (but *only* if the appropriate headers are
included).

rand() takes no arguments, and returns an int result. The result is a
pseudo-random integer in the range 0 to RAND_MAX. RAND_MAX is
guaranteed to be at least 32767.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
Many implementations of rand() are extremely poor.
And the Microsoft's one is particularly hideous. It just isn't suitable
for anything else that toying around.

I suggest you read Knuth's Seminumerical Algorithms to begin with.

Nov 14 '05 #5
> > Thanks both. The chance of me being able to reproduce code on a console
that does anything except draw scorn from a compiler is vanishingly
small.
Attachments are strongly discouraged, binary attachments even more so.


Thank you again for your thoughtful response. My shortcomings with
srand(time(&tim er)) were amended in my binary attachment, which, along with
the .eml it came with, seems to have disappeared. Anscheinend ist Ashcroft
nicht der Einzige, der mir die Post verweigert:-) Is the no binaries policy
to keep the porno punks out?

What to do? I won't read K&R. I did pay $2^5 for the last book I bought on
comp sci. I would like to announce that my demographic and I would gladly
pay $2^6 for a book entitled: Essential Knuth in C. MPJ

-----
All right
Send lawyers, guns and money
Huh!
--Warren Zevon
Nov 14 '05 #6
"Merrill & Michele" <be********@com cast.net> writes:
> Thanks both. The chance of me being able to reproduce code on a console
> that does anything except draw scorn from a compiler is vanishingly
small.
Attachments are strongly discouraged, binary attachments even more so.


Thank you again for your thoughtful response. My shortcomings with
srand(time(&tim er)) were amended in my binary attachment, which, along with
the .eml it came with, seems to have disappeared. Anscheinend ist Ashcroft
nicht der Einzige, der mir die Post verweigert:-) Is the no binaries policy
to keep the porno punks out?


Many of us use newsreaders that don't support binary attachments.
There are newsgroups for binaries; many servers don't carry them
because of the huge bandwidth and storage requirements. This is a
discussion forum. If you want the web, you know where to find it.
What to do? I won't read K&R. I did pay $2^5 for the last book I bought on
comp sci. I would like to announce that my demographic and I would gladly
pay $2^6 for a book entitled: Essential Knuth in C. MPJ


You won't read K&R? Why on Earth not? I find it difficult to believe
that anyone who refuses to read K&R is at all serious about learning C.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
Merrill & Michele wrote:
.... snip ... cosmology. I've attached a screen dump that shows the situation
without showing too much bad programming form. (Do people prefer
to see code pasted into the body of a message or as an attachment?) .... snip ...
Name: ccode1.JPG
ccode1.JPG Type: JPEG Image (image/jpeg)
Encoding: x-uuencode


NEVER attach a binary to a usenet message. In fact, never attach
anything. Cut your code down to the minimum to show the problem,
and remain compilable (not over about 100 lines) and paste that
into the message.

--
"This is a wonderful answer. It's off-topic, it's incorrect,
and it doesn't answer the question." -- Richard Heathfield

"I support the Red Sox and any team that beats the Yankees"
Nov 14 '05 #8
> You won't read K&R? Why on Earth not? I find it difficult to believe
that anyone who refuses to read K&R is at all serious about learning C.


I can't imagine you're interested in my biography, but I'll tell you a
little bit about it. I used to be a champion speller. Then I became
bilingual, trilingual, learned basic, pascal, fortran, c and so forth (not
in that order). Now I can't spell. It turns out, when you know the
Mittelhochdeuts ch and go cross-eyed in cyrillic, it's a one-way street to
missing skills one formerly had.

K&R have **plenty** of influence around here. If the absent demigods want
to step up and be leaders, now would be a good time. MPJ

P.S. My apologies in advance if K or R is unwell.
Nov 14 '05 #9
"Merrill & Michele" <be********@com cast.net> writes:
I can't imagine you're interested in my biography, but I'll tell you a
little bit about it. I used to be a champion speller. Then I became
bilingual, trilingual, learned basic, pascal, fortran, c and so forth (not
in that order). Now I can't spell. It turns out, when you know the
Mittelhochdeuts ch and go cross-eyed in cyrillic, it's a one-way street to
missing skills one formerly had.

K&R have **plenty** of influence around here. If the absent demigods want
to step up and be leaders, now would be a good time. MPJ

P.S. My apologies in advance if K or R is unwell.


I nominate the above for "Weird comp.lang.c Article of the Month".
--
Ben Pfaff
email: bl*@cs.stanford .edu
web: http://benpfaff.org
Nov 14 '05 #10

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

Similar topics

14
653
by: David Shaw | last post by:
Hi, I'm writing a fun little program to play "Petals Around the Roses" with... I need to seed my random numbers so that they won't be the same every time I run the program, but my compiler won't let me. Here's the output my compiler generated...: -- Compiler: Default compiler Executing g++.exe... g++.exe "C:\Documents and Settings\rmarkoff\Desktop\roses.cpp" -o "C:\Documents and Settings\rmarkoff\Desktop\roses.exe"
1
30939
by: Intaek LIM | last post by:
generally, we use srand(time(0)) to generate random numbers. i know why we use time(0), but i can not explain how it operates. first, see example source below. --------------------------------------------- #include <stdio.h> #include <stdlib.h> int main(int argc, char *argv)
13
3077
by: Jeremy Holdstadt | last post by:
This seems to be a C question to me. If it is not, I apologize. This command: awk 'BEGIN {srand();print srand()}' will give the number of seconds since the epoch, present time. Can any of you tell me how to get the number of seconds since the epoch for an arbitrary post-epoch date like Jan 25 1980 13:34 GMT?
3
2180
by: bobrics | last post by:
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
11
7723
by: jtagpgmr | last post by:
I am currently using the gcc compiler on a cygwin platform, I am a beginner when it comes to programming in C and want to know why anytime I run the .exe with the following code I get a "segmentation fault (core dumped)" error: #include <stdio.h> main() {
9
6611
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() {
2
2141
by: Mara Guida | last post by:
"Each time I run my program, I get the same sequence of numbers back from rand()." The answer to question 13.17, in the c-faq is: #include <stdlib.h> #include <time.h> srand((unsigned int)time((time_t *)NULL));
8
887
by: Ioannis Vranos | last post by:
Is srand(time(0)); an effective solution for seeding rand(), or is there any better approach?
12
2916
by: Bill Cunningham | last post by:
I have read and studied and looked at references and can't find out how to do this. Here's where some real knowledge comes in now lets see who in clc really knows there stuff ;) main(void) { srand(time(NULL)); printf("%i\\n",rand()); return 0; Simple and forward now here's where I'm stumped. I want the random values
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10369
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9186
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.