Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8126313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T07:06:33+00:00 2026-06-06T07:06:33+00:00

This is not homework . I am interested in setting up a simulation of

  • 0

This is not homework. I am interested in setting up a simulation of a coin toss in R. I would like to run the simulation for a week. Is there a function in R that will allow me to start and stop the simulation over a time period such as a week? If all goes well, I may want to increase the length of the simulation period.

For example:

x <- rbinom(10, 1, 1/2)

So to clarify, instead of 10 in the code above, how do I keep the simulation going for a week (number of trials in a week versus set number of trials)? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T07:06:34+00:00Added an answer on June 6, 2026 at 7:06 am

    Here is code that will continue to run for three seconds, then stop and print the totals.

    x <- Sys.time()
    duration <- 3 # number of seconds
    heads <- 0
    tails <- 0
    
    while(Sys.time() <= x + duration){
      s <- sample(0:1, 1)
      if(s == 1) heads <- heads+1 else tails <- tails+1
      cat(sample(0:1, 1))
    }
    cat("heads: ", heads)
    cat("tails: ", tails)
    

    The results:

    001100111000011010000010110111111001011110100110001101101010 ...
    heads:  12713
    tails:  12836
    

    Note of warning:

    At the speed of my machine, I bet that you get a floating point error long before the end of the week. In other words, you may get to the maximum value your machine allows you to store as an integer, double, float or whatever you are using, and then your code will crash.

    So you may have to build in some error checking or rollover mechanism to protect you from this.


    For an accelerated illustration of what will happen, try the following:

    x <- 1e300
    while(is.finite(x)){
      x <- x+x
      cat(x, "\n")
    }
    

    R deals with the floating point overload gracefully, and returns Inf.

    So, whatever data you had in the simulation is now lost. It’s not possible to analyse infinity to any sensible degree.

    Keep this in mind when you design your simulation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right off the bat - no, this is NOT homework. I would like to
I would like to ask some help. This is not homework but it is
Please note that this is not homework and i did search before starting this
this one is not homework, it just the fact that i've been out of
This IS NOT homework, but it is a practice that the teacher gave us
[FYI, this is not homework — I guess everyone thinks that because I made
I'm looking at all different sorts. Note that this is not homework (I'm in
I assure you all that this is not homework. I have a combo box:
First off, let me say that this is not homework (I am an A-Level
This is not homework, I need this for my program :) I ask this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.