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 8580753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:53:40+00:00 2026-06-11T20:53:40+00:00

I would like to generate random numbers in my __device__ function, and keep it

  • 0

I would like to generate random numbers in my __device__ function, and keep it in my int Board[500] , I found some examples , but they used some type named curandState. I only need a function like a rand() in C++.

  • 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-11T20:53:41+00:00Added an answer on June 11, 2026 at 8:53 pm

    here is my code , I have N3[40000] array in my device memory , I generate some random numbers in my kernel working for one thread (I mean this “kernel <<<1,1>>> … “) , then I copy it to my N2[40000] from CPU, and print it ,so here is the code

    #include <iostream>
    #include <Cuda.h>
    #include<curand.h>
    #include<curand_kernel.h>
    
    
    int n = 200;
    using namespace std;
    
    __device__ float generate( curandState* globalState, int ind ) 
    {
        //int ind = threadIdx.x;
        curandState localState = globalState[ind];
        float RANDOM = curand_uniform( &localState );
        globalState[ind] = localState;
        return RANDOM;
    }
    
    __global__ void setup_kernel ( curandState * state, unsigned long seed )
    {
        int id = threadIdx.x;
        curand_init ( seed, id, 0, &state[id] );
    }
    
    __global__ void kernel(float* N, curandState* globalState, int n)
    {
        // generate random numbers
        for(int i=0;i<40000;i++)
        {
            int k = generate(globalState, i) * 100000;
            while(k > n*n-1)
            {
                k-=(n*n-1);
            }
            N[i] = k;
        }
    }
    
    int main() 
    {
        int N=40000;
    
        curandState* devStates;
        cudaMalloc ( &devStates, N*sizeof( curandState ) );
    
        // setup seeds
        setup_kernel <<< 1, N >>> ( devStates,unsigned(time(NULL)) );
    
        float N2[40000];
        float* N3;
        cudaMalloc((void**) &N3, sizeof(float)*N);
    
        kernel<<<1,1>>> (N3, devStates, n);
    
        cudaMemcpy(N2, N3, sizeof(float)*N, cudaMemcpyDeviceToHost);
    
        for(int i=0;i<N;i++)
        {
            cout<<N2[i]<<endl;
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How does the TI-84 randInt function generate random numbers? I would like to replicate
I would like to generate some random numbers log-normally distributed with a specified geometric
I am creating a function that I would like to generate random strings from
I would like to use newId to generate random numbers. Usually you would use
I would like to generate some pseudorandom numbers and up until now I've been
I would like to generate random numbers between 1 & 10, with a decimal
I would like to generate uniform random numbers in C++ between 0 and 1,
I need to generate random text strings of a particular format. Would like some
i would like to generate a very simple report with some images and text
Given a regexp, I would like to generate random data x number of time

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.